Make WordPress Core

Changeset 41334


Ignore:
Timestamp:
09/04/2017 04:00:28 PM (9 years ago)
Author:
adamsilverstein
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/wp-api.js

    r41112 r41334  
    3232        wp.api = wp.api || {};
    3333        wp.api.utils = wp.api.utils || {};
     34
     35        /**
     36         * Determine model based on API route.
     37         *
     38         * @param {string} route    The API route.
     39         *
     40         * @return {Backbone Model} The model found at given route. Undefined if not found.
     41         */
     42        wp.api.getModelByRoute = function( route ) {
     43                return _.find( wp.api.models, function( model ) {
     44                        return model.prototype.route && route === model.prototype.route.index;
     45                } );
     46        };
     47
     48        /**
     49         * Determine collection based on API route.
     50         *
     51         * @param {string} route    The API route.
     52         *
     53         * @return {Backbone Model} The collection found at given route. Undefined if not found.
     54         */
     55        wp.api.getCollectionByRoute = function( route ) {
     56                return _.find( wp.api.collections, function( collection ) {
     57                        return collection.prototype.route && route === collection.prototype.route.index;
     58                } );
     59        };
     60
    3461
    3562        /**
  • trunk/tests/qunit/wp-includes/js/wp-api.js

    r41112 r41334  
    195195        } );
    196196
     197        // Find models by route.
     198        var modelsToFetchByRoute = [
     199                'Category',
     200                'Comment',
     201                'Media',
     202                'Page',
     203                'PageRevision',
     204                'Post',
     205                'PostRevision',
     206                'Status',
     207                'Tag',
     208                'Taxonomy',
     209                'Type',
     210                'User'
     211        ];
     212
     213        _.each( modelsToFetchByRoute, function( model ) {
     214                QUnit.test( 'Test fetching ' + model + ' by route.', function( assert ) {
     215
     216                        var done = assert.async();
     217
     218                        assert.expect( 1 );
     219
     220                        wp.api.loadPromise.done( function() {
     221
     222                                var theModel = wp.api.models[ model ];
     223                                var route = theModel.prototype.route.index;
     224
     225                                assert.equal(
     226                                        wp.api.getModelByRoute( route ),
     227                                        theModel,
     228                                        'wp.api.models.' + model + ' found at route ' + route
     229                                );
     230
     231                                // Trigger Qunit async completion.
     232                                done();
     233                        } );
     234                } );
     235        } );
     236
     237        // Find collections by route.
     238        var collectionsToFetchByRoute = [
     239                'Categories',
     240                'Comments',
     241                'Media',
     242                'PageRevisions',
     243                'Pages',
     244                'PostRevisions',
     245                'Posts',
     246                'Statuses',
     247                'Tags',
     248                'Taxonomies',
     249                'Types',
     250                'Users'
     251        ];
     252
     253        _.each( collectionsToFetchByRoute, function( collection ) {
     254                QUnit.test( 'Test fetching ' + collection + ' by route.', function( assert ) {
     255
     256                        var done = assert.async();
     257
     258                        assert.expect( 1 );
     259
     260                        wp.api.loadPromise.done( function() {
     261
     262                                var theCollection = wp.api.collections[ collection ];
     263                                var route = theCollection.prototype.route.index;
     264
     265                                assert.equal(
     266                                        wp.api.getCollectionByRoute( route ),
     267                                        theCollection,
     268                                        'wp.api.collections.' + collection + ' found at ' + route
     269                                );
     270
     271                                // Trigger Qunit async completion.
     272                                done();
     273                        } );
     274                } );
     275        } );
     276
    197277        // Test the jswidget custom namespace and endpoints.
    198278        wp.api.init( {
Note: See TracChangeset for help on using the changeset viewer.