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.


git-svn-id: https://develop.svn.wordpress.org/trunk@41112 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White
2017-07-20 20:25:28 +00:00
parent c66c90a438
commit 79bbfd19c9
3 changed files with 19 additions and 11 deletions
+11 -7
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;
}