WP-API JS Client: Add helpers to get a model or collection by route.

Add two new helper functions, `wp.api.getModelByRoute` and `wp.api.getCollectionByRoute`. Passed a route, they return the matching model or collection, or `undefined` if none is found.

Also adds tests to verify these functions work as expected.

Props rcutmore.
Fixes #41111.

Built from https://develop.svn.wordpress.org/trunk@41334


git-svn-id: http://core.svn.wordpress.org/trunk@41165 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Adam Silverstein 2017-09-04 16:01:43 +00:00
parent f66864af2e
commit ba0970ab28
3 changed files with 29 additions and 2 deletions

View File

@ -32,6 +32,33 @@
wp.api = wp.api || {};
wp.api.utils = wp.api.utils || {};
/**
* Determine model based on API route.
*
* @param {string} route The API route.
*
* @return {Backbone Model} The model found at given route. Undefined if not found.
*/
wp.api.getModelByRoute = function( route ) {
return _.find( wp.api.models, function( model ) {
return model.prototype.route && route === model.prototype.route.index;
} );
};
/**
* Determine collection based on API route.
*
* @param {string} route The API route.
*
* @return {Backbone Model} The collection found at given route. Undefined if not found.
*/
wp.api.getCollectionByRoute = function( route ) {
return _.find( wp.api.collections, function( collection ) {
return collection.prototype.route && route === collection.prototype.route.index;
} );
};
/**
* ECMAScript 5 shim, adapted from MDN.
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.9-alpha-41333';
$wp_version = '4.9-alpha-41334';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.