Make WordPress Core

Ticket #39683: 39683.diff

File 39683.diff, 3.6 KB (added by adamsilverstein, 10 years ago)
  • src/wp-includes/js/wp-api.js

     
    10951095                        /**
    10961096                         * Tracking objects for models and collections.
    10971097                         */
    1098                         loadingObjects.models      = routeModel.get( 'models' );
    1099                         loadingObjects.collections = routeModel.get( 'collections' );
     1098                        loadingObjects.models      = {};
     1099                        loadingObjects.collections = {};
    11001100
    11011101                        _.each( routeModel.schemaModel.get( 'routes' ), function( route, index ) {
    11021102
     
    12661266                                        loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( {
    12671267
    12681268                                                // For the url of a root level collection, use a string.
    1269                                                 url: routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) + routeName,
     1269                                                url: function() {
     1270                                                        return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) + routeName;
     1271                                                },
    12701272
    12711273                                                // Specify the model that this collection contains.
    12721274                                                model: function( attrs, options ) {
     
    12931295                                loadingObjects.models[ index ] = wp.api.utils.addMixinsAndHelpers( model, index, loadingObjects );
    12941296                        } );
    12951297
     1298                        // Set the routeModel models and collections.
     1299                        routeModel.set( 'models', loadingObjects.models );
     1300                        routeModel.set( 'collections', loadingObjects.collections );
     1301
    12961302                }
    12971303
    12981304        } );
    12991305
    1300         wp.api.endpoints = new Backbone.Collection( {
    1301                 model: Endpoint
    1302         } );
     1306        wp.api.endpoints = new Backbone.Collection();
    13031307
    13041308        /**
    13051309         * Initialize the wp-api, optionally passing the API root.
     
    13131317                var endpoint, attributes = {}, deferred, promise;
    13141318
    13151319                args                     = args || {};
    1316                 attributes.apiRoot       = args.apiRoot || wpApiSettings.root;
    1317                 attributes.versionString = args.versionString || wpApiSettings.versionString;
     1320                attributes.apiRoot       = args.apiRoot || wpApiSettings.root || '/wp-json';
     1321                attributes.versionString = args.versionString || wpApiSettings.versionString || 'wp/v2/';
    13181322                attributes.schema        = args.schema || null;
    13191323                if ( ! attributes.schema && attributes.apiRoot === wpApiSettings.root && attributes.versionString === wpApiSettings.versionString ) {
    13201324                        attributes.schema = wpApiSettings.schema;
     
    13211325                }
    13221326
    13231327                if ( ! initializedDeferreds[ attributes.apiRoot + attributes.versionString ] ) {
    1324                         endpoint = wp.api.endpoints.findWhere( { apiRoot: attributes.apiRoot, versionString: attributes.versionString } );
     1328
     1329                        // Look for an existing copy of this endpoint
     1330                        endpoint = wp.api.endpoints.findWhere( { 'apiRoot': attributes.apiRoot, 'versionString': attributes.versionString } );
    13251331                        if ( ! endpoint ) {
    13261332                                endpoint = new Endpoint( attributes );
    1327                                 wp.api.endpoints.add( endpoint );
    13281333                        }
    13291334                        deferred = jQuery.Deferred();
    13301335                        promise = deferred.promise();
    13311336
    1332                         endpoint.schemaConstructed.done( function( endpoint ) {
     1337                        endpoint.schemaConstructed.done( function( resolvedEndpoint ) {
     1338                                wp.api.endpoints.add( resolvedEndpoint );
    13331339
    13341340                                // Map the default endpoints, extending any already present items (including Schema model).
    1335                                 wp.api.models      = _.extend( endpoint.get( 'models' ), wp.api.models );
    1336                                 wp.api.collections = _.extend( endpoint.get( 'collections' ), wp.api.collections );
    1337                                 deferred.resolveWith( wp.api, [ endpoint ] );
     1341                                wp.api.models      = _.extend( wp.api.models, resolvedEndpoint.get( 'models' ) );
     1342                                wp.api.collections = _.extend( wp.api.collections, resolvedEndpoint.get( 'collections' ) );
     1343                                deferred.resolve( resolvedEndpoint );
    13381344                        } );
    13391345                        initializedDeferreds[ attributes.apiRoot + attributes.versionString ] = promise;
    13401346                }