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.


git-svn-id: https://develop.svn.wordpress.org/trunk@41334 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein 2017-09-04 16:00:28 +00:00
parent 503562e03a
commit 399db7ac03
2 changed files with 107 additions and 0 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

View File

@ -194,6 +194,86 @@
} );
} );
// Find models by route.
var modelsToFetchByRoute = [
'Category',
'Comment',
'Media',
'Page',
'PageRevision',
'Post',
'PostRevision',
'Status',
'Tag',
'Taxonomy',
'Type',
'User'
];
_.each( modelsToFetchByRoute, function( model ) {
QUnit.test( 'Test fetching ' + model + ' by route.', function( assert ) {
var done = assert.async();
assert.expect( 1 );
wp.api.loadPromise.done( function() {
var theModel = wp.api.models[ model ];
var route = theModel.prototype.route.index;
assert.equal(
wp.api.getModelByRoute( route ),
theModel,
'wp.api.models.' + model + ' found at route ' + route
);
// Trigger Qunit async completion.
done();
} );
} );
} );
// Find collections by route.
var collectionsToFetchByRoute = [
'Categories',
'Comments',
'Media',
'PageRevisions',
'Pages',
'PostRevisions',
'Posts',
'Statuses',
'Tags',
'Taxonomies',
'Types',
'Users'
];
_.each( collectionsToFetchByRoute, function( collection ) {
QUnit.test( 'Test fetching ' + collection + ' by route.', function( assert ) {
var done = assert.async();
assert.expect( 1 );
wp.api.loadPromise.done( function() {
var theCollection = wp.api.collections[ collection ];
var route = theCollection.prototype.route.index;
assert.equal(
wp.api.getCollectionByRoute( route ),
theCollection,
'wp.api.collections.' + collection + ' found at ' + route
);
// Trigger Qunit async completion.
done();
} );
} );
} );
// Test the jswidget custom namespace and endpoints.
wp.api.init( {
'versionString': 'js-widgets/v1/'