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.

Merges [41112] onto 4.8 branch.
Props adamsilverstein, kadamwhite.
Fixes #41056 for 4.8.1.

Built from https://develop.svn.wordpress.org/branches/4.8@41126


git-svn-id: http://core.svn.wordpress.org/branches/4.8@40966 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2017-07-24 20:46:38 +00:00
parent 96da852a31
commit 6be40b7a13
3 changed files with 13 additions and 9 deletions

View File

@ -1112,7 +1112,10 @@
'PostsRevisions': 'PostRevisions',
'PostsTags': 'PostTags'
}
};
},
modelEndpoints = routeModel.get( 'modelEndpoints' ),
modelRegex = new RegExp( '(?:.*[+)]|\/(' + modelEndpoints.join( '|' ) + '))$' );
/**
* Iterate thru the routes, picking up models and collections to build. Builds two arrays,
@ -1137,8 +1140,8 @@
index !== ( '/' + routeModel.get( 'versionString' ).slice( 0, -1 ) )
) {
// Single items end with a regex (or the special case 'me').
if ( /(?:.*[+)]|\/me)$/.test( index ) ) {
// Single items end with a regex, or a special case word.
if ( modelRegex.test( index ) ) {
modelRoutes.push( { index: index, route: route } );
} else {
@ -1360,10 +1363,11 @@
wp.api.init = function( args ) {
var endpoint, attributes = {}, deferred, promise;
args = args || {};
attributes.apiRoot = args.apiRoot || wpApiSettings.root || '/wp-json';
attributes.versionString = args.versionString || wpApiSettings.versionString || 'wp/v2/';
attributes.schema = args.schema || null;
args = args || {};
attributes.apiRoot = args.apiRoot || wpApiSettings.root || '/wp-json';
attributes.versionString = args.versionString || wpApiSettings.versionString || 'wp/v2/';
attributes.schema = args.schema || null;
attributes.modelEndpoints = args.modelEndpoints || [ 'me', 'settings' ];
if ( ! attributes.schema && attributes.apiRoot === wpApiSettings.root && attributes.versionString === wpApiSettings.versionString ) {
attributes.schema = wpApiSettings.schema;
}

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.8.1-beta1-41117';
$wp_version = '4.8.1-beta1-41126';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.