Changeset 40074
- Timestamp:
- 02/17/2017 08:53:57 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/js/wp-api.js
r40040 r40074 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;
Note: See TracChangeset
for help on using the changeset viewer.