diff --git a/js/load.js b/js/load.js
index 5604999..0c3dacd 100644
|
a
|
b
|
|
| 164 | 164 | |
| 165 | 165 | // Extract the name and any parent from the route. |
| 166 | 166 | 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 |
| 170 | 179 | |
| 171 | 180 | // Handle the special case of the 'me' route. |
| 172 | 181 | if ( 'me' === routeEnd ) { |
| … |
… |
|
| 175 | 184 | |
| 176 | 185 | // If the model has a parent in its route, add that to its class name. |
| 177 | 186 | 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 ); |
| 179 | 188 | modelClassName = mapping.models[ modelClassName ] || modelClassName; |
| 180 | 189 | loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( { |
| 181 | 190 | |
| 182 | 191 | // Return a constructed url based on the parent and id. |
| 183 | 192 | 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 + '/' + |
| 186 | 197 | ( ( _.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 | |
| 190 | 202 | if ( ! _.isUndefined( this.get( 'id' ) ) ) { |
| 191 | 203 | url += '/' + this.get( 'id' ); |
| 192 | 204 | } |
| … |
… |
|
| 222 | 234 | } else { |
| 223 | 235 | |
| 224 | 236 | // This is a model without a parent in its route |
| 225 | | modelClassName = wp.api.utils.capitalize( routeName ); |
| | 237 | modelClassName = wp.api.utils.capitalizeAndCamelCaseDashes( routeName ); |
| 226 | 238 | modelClassName = mapping.models[ modelClassName ] || modelClassName; |
| 227 | 239 | loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( { |
| 228 | 240 | |
| … |
… |
|
| 250 | 262 | } |
| 251 | 263 | |
| 252 | 264 | // 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 | ); |
| 254 | 270 | |
| 255 | 271 | } ); |
| 256 | 272 | |
| … |
… |
|
| 264 | 280 | // Extract the name and any parent from the route. |
| 265 | 281 | var collectionClassName, modelClassName, |
| 266 | 282 | 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 ); |
| 268 | 284 | |
| 269 | 285 | // 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 ) { |
| 271 | 287 | |
| 272 | | collectionClassName = wp.api.utils.capitalize( parentName ) + wp.api.utils.capitalize( routeName ); |
| | 288 | collectionClassName = wp.api.utils.capitalizeAndCamelCaseDashes( parentName ) + wp.api.utils.capitalizeAndCamelCaseDashes( routeName ); |
| 273 | 289 | modelClassName = mapping.models[ collectionClassName ] || collectionClassName; |
| 274 | 290 | collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName; |
| 275 | 291 | loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( { |
| … |
… |
|
| 298 | 314 | } else { |
| 299 | 315 | |
| 300 | 316 | // This is a collection without a parent in its route. |
| 301 | | collectionClassName = wp.api.utils.capitalize( routeName ); |
| | 317 | collectionClassName = wp.api.utils.capitalizeAndCamelCaseDashes( routeName ); |
| 302 | 318 | modelClassName = mapping.models[ collectionClassName ] || collectionClassName; |
| 303 | 319 | collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName; |
| 304 | 320 | loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( { |
diff --git a/js/utils.js b/js/utils.js
index 51f7fd4..4d1abc5 100644
|
a
|
b
|
|
| 96 | 96 | }; |
| 97 | 97 | |
| 98 | 98 | /** |
| | 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 | /** |
| 99 | 121 | * Extract a route part based on negative index. |
| 100 | 122 | * |
| 101 | 123 | * @param {string} route The endpoint route. |
| 102 | 124 | * @param {int} part The number of parts from the end of the route to retrieve. Default 1. |
| 103 | 125 | * 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; |
| 104 | 128 | */ |
| 105 | | wp.api.utils.extractRoutePart = function( route, part ) { |
| | 129 | wp.api.utils.extractRoutePart = function( route, part, versionString, reverse ) { |
| 106 | 130 | var routeParts; |
| 107 | 131 | |
| 108 | | part = part || 1; |
| | 132 | part = part || 1; |
| | 133 | versionString = versionString || wp.api.versionString; |
| 109 | 134 | |
| 110 | 135 | // 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 | } |
| 113 | 144 | if ( _.isUndefined( routeParts[ --part ] ) ) { |
| 114 | 145 | return ''; |
| 115 | 146 | } |
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 ) { |
| 13 | 13 | |
| 14 | 14 | } ); |
| 15 | 15 | |
| | 16 | // Test custom namespaces are parsed correctly. |
| | 17 | wp.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 | |
| 16 | 54 | |
| 17 | 55 | // Verify collections loaded. |
| 18 | 56 | var collectionClassNames = [ |