WordPress.org

Make WordPress Core

Changeset 40364


Ignore:
Timestamp:
04/02/17 19:40:37 (3 months ago)
Author:
adamsilverstein
Message:

REST API: JS Client - Enable connecting to multiple endpoints.

Enable connecting to multiple wp-api endpoints. Calling wp.api.init with a new apiRoot will parse the new endpoint's schema and store a new set of models and collections. A collection of connected endpoints is stored in wp.api.endpoints.

Props lucasstark.
Fixes #39683.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/js/wp-api.js

    r40074 r40364  
    11271127             * Tracking objects for models and collections. 
    11281128             */ 
    1129             loadingObjects.models      = routeModel.get( 'models' ); 
    1130             loadingObjects.collections = routeModel.get( 'collections' ); 
     1129            loadingObjects.models      = {}; 
     1130            loadingObjects.collections = {}; 
    11311131 
    11321132            _.each( routeModel.schemaModel.get( 'routes' ), function( route, index ) { 
     
    13111311 
    13121312                        // 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                        }, 
    13141316 
    13151317                        // Specify the model that this collection contains. 
     
    13381340            } ); 
    13391341 
     1342            // Set the routeModel models and collections. 
     1343            routeModel.set( 'models', loadingObjects.models ); 
     1344            routeModel.set( 'collections', loadingObjects.collections ); 
     1345 
    13401346        } 
    13411347 
    13421348    } ); 
    13431349 
    1344     wp.api.endpoints = new Backbone.Collection( { 
    1345         model: Endpoint 
    1346     } ); 
     1350    wp.api.endpoints = new Backbone.Collection(); 
    13471351 
    13481352    /** 
     
    13581362 
    13591363        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/'; 
    13621366        attributes.schema        = args.schema || null; 
    13631367        if ( ! attributes.schema && attributes.apiRoot === wpApiSettings.root && attributes.versionString === wpApiSettings.versionString ) { 
     
    13661370 
    13671371        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 } ); 
    13691375            if ( ! endpoint ) { 
    13701376                endpoint = new Endpoint( attributes ); 
    1371                 wp.api.endpoints.add( endpoint ); 
    13721377            } 
    13731378            deferred = jQuery.Deferred(); 
    13741379            promise = deferred.promise(); 
    13751380 
    1376             endpoint.schemaConstructed.done( function( endpoint ) { 
     1381            endpoint.schemaConstructed.done( function( resolvedEndpoint ) { 
     1382                wp.api.endpoints.add( resolvedEndpoint ); 
    13771383 
    13781384                // 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 ); 
    13821388            } ); 
    13831389            initializedDeferreds[ attributes.apiRoot + attributes.versionString ] = promise; 
  • trunk/tests/qunit/fixtures/wp-api.js

    r40109 r40364  
    3333 * @param  {object} param The parameters sent to the ajax request. 
    3434 * 
    35  * @return {Object}       A jQuery defered object that resolves with the mapped data. 
     35 * @return {Object}       A jQuery deferred object that resolves with the mapped data. 
    3636 */ 
    3737Backbone.ajax = function ( param ) { 
    3838 
    3939    var data, 
    40         request = param.url.replace( 'http://localhost/', '' ); 
     40        request = param.url 
     41            .replace( 'http://remotehost/', '' ) 
     42            .replace( 'http://localhost/', '' ); 
    4143 
    4244    if ( pathToData[ request ] ) { 
  • trunk/tests/qunit/wp-includes/js/wp-api.js

    r40109 r40364  
    193193    } ); 
    194194 
    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() { 
    199199        var customClasses = [ 
    200200            'WidgetsArchives', 
     
    233233    } ); 
    234234 
     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 
    235264} )( window.QUnit ); 
Note: See TracChangeset for help on using the changeset viewer.