Changeset 40364
- Timestamp:
- 04/02/2017 07:40:37 PM (8 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/js/wp-api.js
r40074 r40364 1127 1127 * Tracking objects for models and collections. 1128 1128 */ 1129 loadingObjects.models = routeModel.get( 'models' );1130 loadingObjects.collections = routeModel.get( 'collections' );1129 loadingObjects.models = {}; 1130 loadingObjects.collections = {}; 1131 1131 1132 1132 _.each( routeModel.schemaModel.get( 'routes' ), function( route, index ) { … … 1311 1311 1312 1312 // For the url of a root level collection, use a string. 1313 url: routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) + routeName, 1313 url: function() { 1314 return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) + routeName; 1315 }, 1314 1316 1315 1317 // Specify the model that this collection contains. … … 1338 1340 } ); 1339 1341 1342 // Set the routeModel models and collections. 1343 routeModel.set( 'models', loadingObjects.models ); 1344 routeModel.set( 'collections', loadingObjects.collections ); 1345 1340 1346 } 1341 1347 1342 1348 } ); 1343 1349 1344 wp.api.endpoints = new Backbone.Collection( { 1345 model: Endpoint 1346 } ); 1350 wp.api.endpoints = new Backbone.Collection(); 1347 1351 1348 1352 /** … … 1358 1362 1359 1363 args = args || {}; 1360 attributes.apiRoot = args.apiRoot || wpApiSettings.root ;1361 attributes.versionString = args.versionString || wpApiSettings.versionString ;1364 attributes.apiRoot = args.apiRoot || wpApiSettings.root || '/wp-json'; 1365 attributes.versionString = args.versionString || wpApiSettings.versionString || 'wp/v2/'; 1362 1366 attributes.schema = args.schema || null; 1363 1367 if ( ! attributes.schema && attributes.apiRoot === wpApiSettings.root && attributes.versionString === wpApiSettings.versionString ) { … … 1366 1370 1367 1371 if ( ! initializedDeferreds[ attributes.apiRoot + attributes.versionString ] ) { 1368 endpoint = wp.api.endpoints.findWhere( { apiRoot: attributes.apiRoot, versionString: attributes.versionString } ); 1372 1373 // Look for an existing copy of this endpoint 1374 endpoint = wp.api.endpoints.findWhere( { 'apiRoot': attributes.apiRoot, 'versionString': attributes.versionString } ); 1369 1375 if ( ! endpoint ) { 1370 1376 endpoint = new Endpoint( attributes ); 1371 wp.api.endpoints.add( endpoint );1372 1377 } 1373 1378 deferred = jQuery.Deferred(); 1374 1379 promise = deferred.promise(); 1375 1380 1376 endpoint.schemaConstructed.done( function( endpoint ) { 1381 endpoint.schemaConstructed.done( function( resolvedEndpoint ) { 1382 wp.api.endpoints.add( resolvedEndpoint ); 1377 1383 1378 1384 // Map the default endpoints, extending any already present items (including Schema model). 1379 wp.api.models = _.extend( endpoint.get( 'models' ), wp.api.models);1380 wp.api.collections = _.extend( endpoint.get( 'collections' ), wp.api.collections);1381 deferred.resolve With( wp.api, [ endpoint ]);1385 wp.api.models = _.extend( wp.api.models, resolvedEndpoint.get( 'models' ) ); 1386 wp.api.collections = _.extend( wp.api.collections, resolvedEndpoint.get( 'collections' ) ); 1387 deferred.resolve( resolvedEndpoint ); 1382 1388 } ); 1383 1389 initializedDeferreds[ attributes.apiRoot + attributes.versionString ] = promise; -
trunk/tests/qunit/fixtures/wp-api.js
r40109 r40364 33 33 * @param {object} param The parameters sent to the ajax request. 34 34 * 35 * @return {Object} A jQuery defer ed object that resolves with the mapped data.35 * @return {Object} A jQuery deferred object that resolves with the mapped data. 36 36 */ 37 37 Backbone.ajax = function ( param ) { 38 38 39 39 var data, 40 request = param.url.replace( 'http://localhost/', '' ); 40 request = param.url 41 .replace( 'http://remotehost/', '' ) 42 .replace( 'http://localhost/', '' ); 41 43 42 44 if ( pathToData[ request ] ) { -
trunk/tests/qunit/wp-includes/js/wp-api.js
r40109 r40364 193 193 } ); 194 194 195 // Test the jswidget custom namespace and endpoints.196 wp.api.init( {197 'versionString': 'js-widgets/v1/'198 } ).done( function() {195 // Test the jswidget custom namespace and endpoints. 196 wp.api.init( { 197 'versionString': 'js-widgets/v1/' 198 } ).done( function() { 199 199 var customClasses = [ 200 200 'WidgetsArchives', … … 233 233 } ); 234 234 235 // Check connecting to a second URL. 236 wp.api.loadPromise.done( function() { 237 QUnit.test( 'Checking connecting to a remote url.' , function( assert ) { 238 var done = assert.async(); 239 240 wp.api.init({ 241 'apiRoot': 'http://remotehost/wp-json/' 242 } ).done( function(){ 243 var lastEndpoint = wp.api.endpoints.last(), 244 models = lastEndpoint.get( 'models' ), 245 post = new models.Post(); 246 247 assert.equal( 'http://remotehost/wp-json/wp/v2/posts', post.url(), 'The remote API objects should have their own URLs' ); 248 249 wp.api.init({ 250 'apiRoot': 'http://localhost/wp-json/' 251 } ).done( function(){ 252 var lastEndpoint = wp.api.endpoints.first(), 253 models = lastEndpoint.get( 'models' ), 254 post = new models.Post(); 255 256 assert.equal( 'http://localhost/wp-json/wp/v2/posts', post.url(), 'The local API objects should have their own URLs' ); 257 258 done(); 259 } ); 260 } ); 261 } ); 262 }); 263 235 264 } )( window.QUnit );
Note: See TracChangeset
for help on using the changeset viewer.