diff --git src/wp-includes/js/wp-api.js src/wp-includes/js/wp-api.js
index 0ce6bbc..3f950a4 100644
|
|
|
1126 | 1126 | /** |
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 ) { |
1133 | 1133 | |
… |
… |
|
1310 | 1310 | loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( { |
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. |
1316 | 1318 | model: function( attrs, options ) { |
… |
… |
|
1337 | 1339 | loadingObjects.models[ index ] = wp.api.utils.addMixinsAndHelpers( model, index, loadingObjects ); |
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 | /** |
1349 | 1353 | * Initialize the wp-api, optionally passing the API root. |
… |
… |
|
1357 | 1361 | var endpoint, attributes = {}, deferred, promise; |
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 ) { |
1364 | 1368 | attributes.schema = wpApiSettings.schema; |
1365 | 1369 | } |
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.resolveWith( 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; |
1384 | 1390 | } |
diff --git tests/qunit/fixtures/wp-api.js tests/qunit/fixtures/wp-api.js
index d9d0804..7b8005e 100644
|
|
var pathToData = { |
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 ] ) { |
43 | 45 | data = pathToData[ request ]; |
diff --git tests/qunit/wp-includes/js/wp-api.js tests/qunit/wp-includes/js/wp-api.js
index acca461..180942f 100644
|
|
|
192 | 192 | } ); |
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', |
201 | 201 | 'WidgetsCalendar', |
… |
… |
wp.api.init( { |
232 | 232 | |
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 ); |