Make WordPress Core

Ticket #39561: 39561.diff

File 39561.diff, 8.1 KB (added by adamsilverstein, 8 years ago)
  • js/load.js

    diff --git a/js/load.js b/js/load.js
    index 5604999..0c3dacd 100644
    a b  
    164164
    165165                                // Extract the name and any parent from the route.
    166166                                var modelClassName,
    167                                                 routeName  = wp.api.utils.extractRoutePart( modelRoute.index, 2 ),
    168                                                 parentName = wp.api.utils.extractRoutePart( modelRoute.index, 4 ),
    169                                                 routeEnd   = wp.api.utils.extractRoutePart( modelRoute.index, 1 );
     167                                        routeName  = wp.api.utils.extractRoutePart( modelRoute.index, 2, routeModel.get( 'versionString' ), true ),
     168                                        parentName = wp.api.utils.extractRoutePart( modelRoute.index, 1, routeModel.get( 'versionString' ), false ),
     169                                        routeEnd   = wp.api.utils.extractRoutePart( modelRoute.index, 1, routeModel.get( 'versionString' ), true );
     170
     171                                // Clear the parent part of the rouite if its actually the version string.
     172                                if ( parentName === routeModel.get( 'versionString' ) ) {
     173                                        parentName = '';
     174                                }
     175                                window.console.log( parentName );
     176
     177                                // Camel case dashed names.
     178//                              routeEnd = routeEnd.replace
    170179
    171180                                // Handle the special case of the 'me' route.
    172181                                if ( 'me' === routeEnd ) {
     
    175184
    176185                                // If the model has a parent in its route, add that to its class name.
    177186                                if ( '' !== parentName && parentName !== routeName ) {
    178                                         modelClassName = wp.api.utils.capitalize( parentName ) + wp.api.utils.capitalize( routeName );
     187                                        modelClassName = wp.api.utils.capitalizeAndCamelCaseDashes( parentName ) + wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
    179188                                        modelClassName = mapping.models[ modelClassName ] || modelClassName;
    180189                                        loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( {
    181190
    182191                                                // Return a constructed url based on the parent and id.
    183192                                                url: function() {
    184                                                         var url = routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) +
    185                                                                         parentName +  '/' +
     193                                                        var url =
     194                                                                routeModel.get( 'apiRoot' ) +
     195                                                                routeModel.get( 'versionString' ) +
     196                                                                parentName +  '/' +
    186197                                                                        ( ( _.isUndefined( this.get( 'parent' ) ) || 0 === this.get( 'parent' ) ) ?
    187                                                                                 this.get( 'parent_post' ) :
    188                                                                                 this.get( 'parent' ) ) + '/' +
    189                                                                         routeName;
     198                                                                                ( _.isUndefined( this.get( 'parent_post' ) ) ? '' : this.get( 'parent_post' ) + '/' ) :
     199                                                                                this.get( 'parent' ) + '/' ) +
     200                                                                routeName;
     201
    190202                                                        if ( ! _.isUndefined( this.get( 'id' ) ) ) {
    191203                                                                url +=  '/' + this.get( 'id' );
    192204                                                        }
     
    222234                                } else {
    223235
    224236                                        // This is a model without a parent in its route
    225                                         modelClassName = wp.api.utils.capitalize( routeName );
     237                                        modelClassName = wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
    226238                                        modelClassName = mapping.models[ modelClassName ] || modelClassName;
    227239                                        loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( {
    228240
     
    250262                                }
    251263
    252264                                // Add defaults to the new model, pulled form the endpoint.
    253                                 wp.api.utils.decorateFromRoute( modelRoute.route.endpoints, loadingObjects.models[ modelClassName ] );
     265                                wp.api.utils.decorateFromRoute(
     266                                        modelRoute.route.endpoints,
     267                                        loadingObjects.models[ modelClassName ],
     268                                        routeModel.get( 'versionString' )
     269                                );
    254270
    255271                        } );
    256272
     
    264280                                // Extract the name and any parent from the route.
    265281                                var collectionClassName, modelClassName,
    266282                                                routeName  = collectionRoute.index.slice( collectionRoute.index.lastIndexOf( '/' ) + 1 ),
    267                                                 parentName = wp.api.utils.extractRoutePart( collectionRoute.index, 3 );
     283                                                parentName = wp.api.utils.extractRoutePart( collectionRoute.index, 1, routeModel.get( 'versionString' ), false );
    268284
    269285                                // If the collection has a parent in its route, add that to its class name.
    270                                 if ( '' !== parentName && parentName !== routeName ) {
     286                                if ( '' !== parentName && parentName !== routeName && routeModel.get( 'versionString' ) !== parentName ) {
    271287
    272                                         collectionClassName = wp.api.utils.capitalize( parentName ) + wp.api.utils.capitalize( routeName );
     288                                        collectionClassName = wp.api.utils.capitalizeAndCamelCaseDashes( parentName ) + wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
    273289                                        modelClassName      = mapping.models[ collectionClassName ] || collectionClassName;
    274290                                        collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName;
    275291                                        loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( {
     
    298314                                } else {
    299315
    300316                                        // This is a collection without a parent in its route.
    301                                         collectionClassName = wp.api.utils.capitalize( routeName );
     317                                        collectionClassName = wp.api.utils.capitalizeAndCamelCaseDashes( routeName );
    302318                                        modelClassName      = mapping.models[ collectionClassName ] || collectionClassName;
    303319                                        collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName;
    304320                                        loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( {
  • js/utils.js

    diff --git a/js/utils.js b/js/utils.js
    index 51f7fd4..4d1abc5 100644
    a b  
    9696        };
    9797
    9898        /**
     99         * Helper function that capitilises the first word and camel cases any words starting
     100         * after dashes, removing the dashes.
     101         */
     102        wp.api.utils.capitalizeAndCamelCaseDashes = function( str ) {
     103                if ( _.isUndefined( str ) ) {
     104                        return str;
     105                }
     106                str = wp.api.utils.capitalize( str );
     107
     108                return wp.api.utils.camelCaseDashes( str );
     109        };
     110
     111        /**
     112         * Helper function to camel case the letter after dashes, removing the dashes.
     113         */
     114        wp.api.utils.camelCaseDashes = function( str ) {
     115                return str.replace( /-([a-z])/g, function( g ) {
     116                        return g[ 1 ].toUpperCase();
     117                } );
     118        };
     119
     120        /**
    99121         * Extract a route part based on negative index.
    100122         *
    101123         * @param {string} route The endpoint route.
    102124         * @param {int}    part  The number of parts from the end of the route to retrieve. Default 1.
    103125         *                       Example route `/a/b/c`: part 1 is `c`, part 2 is `b`, part 3 is `a`.
     126         * @param {string} [versionString] Version string, defaults to wp.api.versionString.
     127         * @param {boolean} [reverse] Whether to reverse the order when extracting the rout part. Optional, default true;
    104128         */
    105         wp.api.utils.extractRoutePart = function( route, part ) {
     129        wp.api.utils.extractRoutePart = function( route, part, versionString, reverse ) {
    106130                var routeParts;
    107131
    108                 part  = part || 1;
     132                part = part || 1;
     133                versionString = versionString || wp.api.versionString;
    109134
    110135                // Remove versions string from route to avoid returning it.
    111                 route = route.replace( wp.api.versionString, '' );
    112                 routeParts = route.split( '/' ).reverse();
     136                if ( 0 === route.indexOf( '/' + versionString ) ) {
     137                        route = route.substr( versionString.length + 1 );
     138                }
     139
     140                routeParts = route.split( '/' );
     141                if ( reverse ) {
     142                        routeParts = routeParts.reverse();
     143                }
    113144                if ( _.isUndefined( routeParts[ --part ] ) ) {
    114145                        return '';
    115146                }
  • tests/wp-api.js

    diff --git a/tests/wp-api.js b/tests/wp-api.js
    index b3e98c0..8931c70 100644
    a b QUnit.test( 'API Loaded correctly', function( assert ) { 
    1313
    1414} );
    1515
     16// Test custom namespaces are parsed correctly.
     17wp.api.init( {
     18        'versionString': 'js-widgets/v1/'
     19} )
     20        .done( function() {
     21                var customModels = [
     22                        'WidgetsText',
     23                        'WidgetsRecentPosts',
     24                        'WidgetsPostCollection'
     25                ];
     26
     27                // Check that we have and can get each model type.
     28                _.each( customModels, function( className ) {
     29                        window.console.log( 'Checking ' + className + ' model.' );
     30                        QUnit.test( 'Checking ' + className + ' model.' , function( assert ) {
     31                                var done = assert.async();
     32
     33                                assert.expect( 2 );
     34
     35                                wp.api.loadPromise.done( function() {
     36                                        var theModel = new wp.api.models[ className ]();
     37                                        assert.ok( theModel, 'We can instantiate wp.api.models.' + className );
     38                                        theModel.fetch().done( function() {
     39                                                var theModel2 = new wp.api.models[ className ]();
     40                                                theModel2.set( 'id', theModel.attributes[0].id );
     41                                                theModel2.fetch().done( function() {
     42                                                        assert.equal( theModel.attributes[0].id, theModel2.get( 'id' ) , 'We should be able to get a ' + className );
     43                                                        done();
     44                                                } );
     45                                        } );
     46
     47                                } );
     48
     49                        });
     50                } );
     51
     52        } );
     53
    1654
    1755// Verify collections loaded.
    1856var collectionClassNames = [