Changeset 40117 for branches/4.7
- Timestamp:
- 02/24/2017 10:47:47 PM (8 years ago)
- Location:
- branches/4.7
- Files:
-
- 5 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/4.7
- Property svn:mergeinfo changed
/trunk merged: 40074,40109
- Property svn:mergeinfo changed
-
branches/4.7/src/wp-includes/js/wp-api.js
r40084 r40117 121 121 122 122 /** 123 * Helper function that capitalizes the first word and camel cases any words starting 124 * after dashes, removing the dashes. 125 */ 126 wp.api.utils.capitalizeAndCamelCaseDashes = function( str ) { 127 if ( _.isUndefined( str ) ) { 128 return str; 129 } 130 str = wp.api.utils.capitalize( str ); 131 132 return wp.api.utils.camelCaseDashes( str ); 133 }; 134 135 /** 136 * Helper function to camel case the letter after dashes, removing the dashes. 137 */ 138 wp.api.utils.camelCaseDashes = function( str ) { 139 return str.replace( /-([a-z])/g, function( g ) { 140 return g[ 1 ].toUpperCase(); 141 } ); 142 }; 143 144 /** 123 145 * Extract a route part based on negative index. 124 146 * 125 * @param {string} route The endpoint route. 126 * @param {int} part The number of parts from the end of the route to retrieve. Default 1. 127 * Example route `/a/b/c`: part 1 is `c`, part 2 is `b`, part 3 is `a`. 128 */ 129 wp.api.utils.extractRoutePart = function( route, part ) { 147 * @param {string} route The endpoint route. 148 * @param {int} part The number of parts from the end of the route to retrieve. Default 1. 149 * Example route `/a/b/c`: part 1 is `c`, part 2 is `b`, part 3 is `a`. 150 * @param {string} [versionString] Version string, defaults to `wp.api.versionString`. 151 * @param {boolean} [reverse] Whether to reverse the order when extracting the route part. Optional, default false. 152 */ 153 wp.api.utils.extractRoutePart = function( route, part, versionString, reverse ) { 130 154 var routeParts; 131 155 132 part = part || 1; 156 part = part || 1; 157 versionString = versionString || wp.api.versionString; 133 158 134 159 // Remove versions string from route to avoid returning it. 135 route = route.replace( wp.api.versionString, '' ); 136 routeParts = route.split( '/' ).reverse(); 160 if ( 0 === route.indexOf( '/' + versionString ) ) { 161 route = route.substr( versionString.length + 1 ); 162 } 163 164 routeParts = route.split( '/' ); 165 if ( reverse ) { 166 routeParts = routeParts.reverse(); 167 } 137 168 if ( _.isUndefined( routeParts[ --part ] ) ) { 138 169 return ''; … … 1127 1158 // Extract the name and any parent from the route. 1128 1159 var modelClassName, 1129 routeName = wp.api.utils.extractRoutePart( modelRoute.index, 2 ), 1130 parentName = wp.api.utils.extractRoutePart( modelRoute.index, 4 ), 1131 routeEnd = wp.api.utils.extractRoutePart( modelRoute.index, 1 ); 1160 routeName = wp.api.utils.extractRoutePart( modelRoute.index, 2, routeModel.get( 'versionString' ), true ), 1161 parentName = wp.api.utils.extractRoutePart( modelRoute.index, 1, routeModel.get( 'versionString' ), false ), 1162 routeEnd = wp.api.utils.extractRoutePart( modelRoute.index, 1, routeModel.get( 'versionString' ), true ); 1163 1164 // Clear the parent part of the rouite if its actually the version string. 1165 if ( parentName === routeModel.get( 'versionString' ) ) { 1166 parentName = ''; 1167 } 1132 1168 1133 1169 // Handle the special case of the 'me' route. … … 1138 1174 // If the model has a parent in its route, add that to its class name. 1139 1175 if ( '' !== parentName && parentName !== routeName ) { 1140 modelClassName = wp.api.utils.capitalize ( parentName ) + wp.api.utils.capitalize( routeName );1176 modelClassName = wp.api.utils.capitalizeAndCamelCaseDashes( parentName ) + wp.api.utils.capitalizeAndCamelCaseDashes( routeName ); 1141 1177 modelClassName = mapping.models[ modelClassName ] || modelClassName; 1142 1178 loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( { … … 1144 1180 // Return a constructed url based on the parent and id. 1145 1181 url: function() { 1146 var url = routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) + 1147 parentName + '/' + 1182 var url = 1183 routeModel.get( 'apiRoot' ) + 1184 routeModel.get( 'versionString' ) + 1185 parentName + '/' + 1148 1186 ( ( _.isUndefined( this.get( 'parent' ) ) || 0 === this.get( 'parent' ) ) ? 1149 this.get( 'parent_post' ) : 1150 this.get( 'parent' ) ) + '/' + 1151 routeName; 1187 ( _.isUndefined( this.get( 'parent_post' ) ) ? '' : this.get( 'parent_post' ) + '/' ) : 1188 this.get( 'parent' ) + '/' ) + 1189 routeName; 1190 1152 1191 if ( ! _.isUndefined( this.get( 'id' ) ) ) { 1153 1192 url += '/' + this.get( 'id' ); … … 1165 1204 methods: modelRoute.route.methods, 1166 1205 1167 initialize: function() { 1206 initialize: function( attributes, options ) { 1207 wp.api.WPApiBaseModel.prototype.initialize.call( this, attributes, options ); 1168 1208 1169 1209 /** … … 1185 1225 1186 1226 // This is a model without a parent in its route 1187 modelClassName = wp.api.utils.capitalize ( routeName );1227 modelClassName = wp.api.utils.capitalizeAndCamelCaseDashes( routeName ); 1188 1228 modelClassName = mapping.models[ modelClassName ] || modelClassName; 1189 1229 loadingObjects.models[ modelClassName ] = wp.api.WPApiBaseModel.extend( { … … 1213 1253 1214 1254 // Add defaults to the new model, pulled form the endpoint. 1215 wp.api.utils.decorateFromRoute( modelRoute.route.endpoints, loadingObjects.models[ modelClassName ] ); 1255 wp.api.utils.decorateFromRoute( 1256 modelRoute.route.endpoints, 1257 loadingObjects.models[ modelClassName ], 1258 routeModel.get( 'versionString' ) 1259 ); 1216 1260 1217 1261 } ); … … 1227 1271 var collectionClassName, modelClassName, 1228 1272 routeName = collectionRoute.index.slice( collectionRoute.index.lastIndexOf( '/' ) + 1 ), 1229 parentName = wp.api.utils.extractRoutePart( collectionRoute.index, 3);1273 parentName = wp.api.utils.extractRoutePart( collectionRoute.index, 1, routeModel.get( 'versionString' ), false ); 1230 1274 1231 1275 // If the collection has a parent in its route, add that to its class name. 1232 if ( '' !== parentName && parentName !== routeName ) {1233 1234 collectionClassName = wp.api.utils.capitalize ( parentName ) + wp.api.utils.capitalize( routeName );1276 if ( '' !== parentName && parentName !== routeName && routeModel.get( 'versionString' ) !== parentName ) { 1277 1278 collectionClassName = wp.api.utils.capitalizeAndCamelCaseDashes( parentName ) + wp.api.utils.capitalizeAndCamelCaseDashes( routeName ); 1235 1279 modelClassName = mapping.models[ collectionClassName ] || collectionClassName; 1236 1280 collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName; … … 1261 1305 1262 1306 // This is a collection without a parent in its route. 1263 collectionClassName = wp.api.utils.capitalize ( routeName );1307 collectionClassName = wp.api.utils.capitalizeAndCamelCaseDashes( routeName ); 1264 1308 modelClassName = mapping.models[ collectionClassName ] || collectionClassName; 1265 1309 collectionClassName = mapping.collections[ collectionClassName ] || collectionClassName; -
branches/4.7/tests/qunit/fixtures/wp-api.js
r40116 r40117 1 /* global mockedApiResponse, Backbone */1 /* global mockedApiResponse, Backbone, jsWidgetsEndpointSchema */ 2 2 /** 3 3 * @var mockedApiResponse defined in wp-api-generated.js … … 24 24 'wp-json/wp/v2/taxonomy': mockedApiResponse.TaxonomyModel, 25 25 'wp-json/wp/v2/status': mockedApiResponse.StatusModel, 26 'wp-json/wp/v2/type': mockedApiResponse.TypeModel 26 'wp-json/wp/v2/type': mockedApiResponse.TypeModel, 27 'wp-json/js-widgets/v1/': jsWidgetsEndpointSchema 27 28 }; 28 29 -
branches/4.7/tests/qunit/index.html
r40116 r40117 41 41 <script src="fixtures/customize-widgets.js"></script> 42 42 <script src="fixtures/wp-api-generated.js"></script> 43 <script src="fixtures/js-widgets-endpoint.js"></script> 43 44 <script src="fixtures/wp-api.js"></script> 44 45 </div> -
branches/4.7/tests/qunit/wp-includes/js/wp-api.js
r40116 r40117 193 193 } ); 194 194 195 // Test the jswidget custom namespace and endpoints. 196 wp.api.init( { 197 'versionString': 'js-widgets/v1/' 198 } ).done( function() { 199 var customClasses = [ 200 'WidgetsArchives', 201 'WidgetsCalendar', 202 'WidgetsCategories', 203 'WidgetsMeta', 204 'WidgetsNav_menu', 205 'WidgetsPages', 206 'WidgetsPostCollection', 207 'WidgetsRecentComments', 208 'WidgetsRecentPosts', 209 'WidgetsRss', 210 'WidgetsSearch', 211 'WidgetsTag_cloud', 212 'WidgetsText' 213 ]; 214 215 // Check that we have and can get each model type. 216 _.each( customClasses, function( className ) { 217 QUnit.test( 'Checking ' + className + ' class name.' , function( assert ) { 218 var done = assert.async(); 219 220 assert.expect( 2 ); 221 222 wp.api.loadPromise.done( function() { 223 var theModel = new wp.api.models[ className ](); 224 assert.ok( theModel, 'We can instantiate wp.api.models.' + className ); 225 var theCollection = new wp.api.collections[ className ](); 226 assert.ok( theCollection, 'We can instantiate wp.api.collections.' + className ); 227 // Trigger Qunit async completion. 228 done(); 229 } ); 230 } ); 231 } ); 232 233 } ); 234 195 235 } )( window.QUnit );
Note: See TracChangeset
for help on using the changeset viewer.