Make WordPress Core

Changeset 41112 for trunk


Ignore:
Timestamp:
07/20/2017 08:25:28 PM (7 years ago)
Author:
kadamwhite
Message:

WP-API JS Client: Interpret Settings resource as a model.

The REST API does not provide a mechanism to distinguish between endpoints representing models and those representing collections, so the Backbone client must make that distinction internally. Previously wp-api.js accounted for /users/me, but not for /settings. This patch updates the logic so that /settings is properly registered as a Backbone model.

When calling wp.api.init, additional endpoints can be specified to be models using the modelEndpoints argument.

Props @adamsilverstein.
Fixes #41056.

Location:
trunk
Files:
3 edited

Legend:

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

    r40364 r41112  
    11131113                    'PostsTags':       'PostTags'
    11141114                }
    1115             };
     1115            },
     1116
     1117            modelEndpoints = routeModel.get( 'modelEndpoints' ),
     1118            modelRegex     = new RegExp( '(?:.*[+)]|\/(' + modelEndpoints.join( '|' ) + '))$' );
    11161119
    11171120            /**
     
    11381141                ) {
    11391142
    1140                     // Single items end with a regex (or the special case 'me').
    1141                     if ( /(?:.*[+)]|\/me)$/.test( index ) ) {
     1143                    // Single items end with a regex, or a special case word.
     1144                    if ( modelRegex.test( index ) ) {
    11421145                        modelRoutes.push( { index: index, route: route } );
    11431146                    } else {
     
    13611364        var endpoint, attributes = {}, deferred, promise;
    13621365
    1363         args                     = args || {};
    1364         attributes.apiRoot       = args.apiRoot || wpApiSettings.root || '/wp-json';
    1365         attributes.versionString = args.versionString || wpApiSettings.versionString || 'wp/v2/';
    1366         attributes.schema        = args.schema || null;
     1366        args                      = args || {};
     1367        attributes.apiRoot        = args.apiRoot || wpApiSettings.root || '/wp-json';
     1368        attributes.versionString  = args.versionString || wpApiSettings.versionString || 'wp/v2/';
     1369        attributes.schema         = args.schema || null;
     1370        attributes.modelEndpoints = args.modelEndpoints || [ 'me', 'settings' ];
    13671371        if ( ! attributes.schema && attributes.apiRoot === wpApiSettings.root && attributes.versionString === wpApiSettings.versionString ) {
    13681372            attributes.schema = wpApiSettings.schema;
  • trunk/tests/qunit/fixtures/wp-api.js

    r40364 r41112  
    2525    'wp-json/wp/v2/status': mockedApiResponse.StatusModel,
    2626    'wp-json/wp/v2/type': mockedApiResponse.TypeModel,
    27     'wp-json/js-widgets/v1/': jsWidgetsEndpointSchema
     27    'wp-json/js-widgets/v1/': jsWidgetsEndpointSchema,
     28    'wp-json/wp/v2/users/me': mockedApiResponse.me,
     29    'wp-json/wp/v2/settings': mockedApiResponse.settings
    2830};
    2931
  • trunk/tests/qunit/wp-includes/js/wp-api.js

    r40364 r41112  
    116116        'Post',
    117117        'Tag',
    118         'User'
     118        'User',
     119        'UsersMe',
     120        'Settings'
    119121    ];
    120122
     
    131133                theModel.fetch().done( function(  ) {
    132134                    var theModel2 = new wp.api.models[ className ]();
    133                     theModel2.set( 'id', theModel.attributes[0].id );
     135                    theModel2.set( 'id', theModel.attributes.id );
    134136                    theModel2.fetch().done( function() {
    135137
    136138                        // We were able to retrieve the model.
    137139                        assert.equal(
    138                             theModel.attributes[0].id,
     140                            theModel.attributes.id,
    139141                            theModel2.get( 'id' ) ,
    140142                            'We should be able to get a ' + className
Note: See TracChangeset for help on using the changeset viewer.