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:
parent
c66c90a438
commit
79bbfd19c9
@ -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;
|
||||
}
|
||||
|
@ -24,7 +24,9 @@ var pathToData = {
|
||||
'wp-json/wp/v2/taxonomy': mockedApiResponse.TaxonomyModel,
|
||||
'wp-json/wp/v2/status': mockedApiResponse.StatusModel,
|
||||
'wp-json/wp/v2/type': mockedApiResponse.TypeModel,
|
||||
'wp-json/js-widgets/v1/': jsWidgetsEndpointSchema
|
||||
'wp-json/js-widgets/v1/': jsWidgetsEndpointSchema,
|
||||
'wp-json/wp/v2/users/me': mockedApiResponse.me,
|
||||
'wp-json/wp/v2/settings': mockedApiResponse.settings
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -115,7 +115,9 @@
|
||||
'Page',
|
||||
'Post',
|
||||
'Tag',
|
||||
'User'
|
||||
'User',
|
||||
'UsersMe',
|
||||
'Settings'
|
||||
];
|
||||
|
||||
_.each( modelsWithIdsClassNames, function( className ) {
|
||||
@ -130,12 +132,12 @@
|
||||
assert.ok( theModel, 'We can instantiate wp.api.models.' + className );
|
||||
theModel.fetch().done( function( ) {
|
||||
var theModel2 = new wp.api.models[ className ]();
|
||||
theModel2.set( 'id', theModel.attributes[0].id );
|
||||
theModel2.set( 'id', theModel.attributes.id );
|
||||
theModel2.fetch().done( function() {
|
||||
|
||||
// We were able to retrieve the model.
|
||||
assert.equal(
|
||||
theModel.attributes[0].id,
|
||||
theModel.attributes.id,
|
||||
theModel2.get( 'id' ) ,
|
||||
'We should be able to get a ' + className
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user