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.

Built from https://develop.svn.wordpress.org/trunk@41112


git-svn-id: http://core.svn.wordpress.org/trunk@40952 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
K. Adam White 2017-07-20 20:26:45 +00:00
parent 970afde4c0
commit 6aaba32b36
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.9-alpha-41111';
$wp_version = '4.9-alpha-41112';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.