diff --git a/src/wp-includes/js/wp-api.js b/src/wp-includes/js/wp-api.js index 634db4357e..c8caf50681 100644 --- a/src/wp-includes/js/wp-api.js +++ b/src/wp-includes/js/wp-api.js @@ -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 diff --git a/tests/qunit/wp-includes/js/wp-api.js b/tests/qunit/wp-includes/js/wp-api.js index a64e406a49..e904ccaec8 100644 --- a/tests/qunit/wp-includes/js/wp-api.js +++ b/tests/qunit/wp-includes/js/wp-api.js @@ -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/'