361 * Build a helper function to retrieve related model. |
361 * Build a helper function to retrieve related model. |
362 * |
362 * |
363 * @param {string} parentModel The parent model. |
363 * @param {string} parentModel The parent model. |
364 * @param {number} modelId The model ID if the object to request |
364 * @param {number} modelId The model ID if the object to request |
365 * @param {string} modelName The model name to use when constructing the model. |
365 * @param {string} modelName The model name to use when constructing the model. |
366 * @param {string} embedSourcePoint Where to check the embedds object for _embed data. |
366 * @param {string} embedSourcePoint Where to check the embedded object for _embed data. |
367 * @param {string} embedCheckField Which model field to check to see if the model has data. |
367 * @param {string} embedCheckField Which model field to check to see if the model has data. |
368 * |
368 * |
369 * @return {Deferred.promise} A promise which resolves to the constructed model. |
369 * @return {Deferred.promise} A promise which resolves to the constructed model. |
370 */ |
370 */ |
371 buildModelGetter = function( parentModel, modelId, modelName, embedSourcePoint, embedCheckField ) { |
371 buildModelGetter = function( parentModel, modelId, modelName, embedSourcePoint, embedCheckField ) { |
372 var getModel, embeddeds, attributes, deferred; |
372 var getModel, embeddedObjects, attributes, deferred; |
373 |
373 |
374 deferred = jQuery.Deferred(); |
374 deferred = jQuery.Deferred(); |
375 embeddeds = parentModel.get( '_embedded' ) || {}; |
375 embeddedObjects = parentModel.get( '_embedded' ) || {}; |
376 |
376 |
377 // Verify that we have a valid object id. |
377 // Verify that we have a valid object id. |
378 if ( ! _.isNumber( modelId ) || 0 === modelId ) { |
378 if ( ! _.isNumber( modelId ) || 0 === modelId ) { |
379 deferred.reject(); |
379 deferred.reject(); |
380 return deferred; |
380 return deferred; |
381 } |
381 } |
382 |
382 |
383 // If we have embedded object data, use that when constructing the getModel. |
383 // If we have embedded object data, use that when constructing the getModel. |
384 if ( embeddeds[ embedSourcePoint ] ) { |
384 if ( embeddedObjects[ embedSourcePoint ] ) { |
385 attributes = _.findWhere( embeddeds[ embedSourcePoint ], { id: modelId } ); |
385 attributes = _.findWhere( embeddedObjects[ embedSourcePoint ], { id: modelId } ); |
386 } |
386 } |
387 |
387 |
388 // Otherwise use the modelId. |
388 // Otherwise use the modelId. |
389 if ( ! attributes ) { |
389 if ( ! attributes ) { |
390 attributes = { id: modelId }; |
390 attributes = { id: modelId }; |
414 /** |
414 /** |
415 * Build a helper to retrieve a collection. |
415 * Build a helper to retrieve a collection. |
416 * |
416 * |
417 * @param {string} parentModel The parent model. |
417 * @param {string} parentModel The parent model. |
418 * @param {string} collectionName The name to use when constructing the collection. |
418 * @param {string} collectionName The name to use when constructing the collection. |
419 * @param {string} embedSourcePoint Where to check the embedds object for _embed data. |
419 * @param {string} embedSourcePoint Where to check the embedded object for _embed data. |
420 * @param {string} embedIndex An addiitonal optional index for the _embed data. |
420 * @param {string} embedIndex An additional optional index for the _embed data. |
421 * |
421 * |
422 * @return {Deferred.promise} A promise which resolves to the constructed collection. |
422 * @return {Deferred.promise} A promise which resolves to the constructed collection. |
423 */ |
423 */ |
424 buildCollectionGetter = function( parentModel, collectionName, embedSourcePoint, embedIndex ) { |
424 buildCollectionGetter = function( parentModel, collectionName, embedSourcePoint, embedIndex ) { |
425 /** |
425 /** |
426 * Returns a promise that resolves to the requested collection |
426 * Returns a promise that resolves to the requested collection |
427 * |
427 * |
428 * Uses the embedded data if available, otherwises fetches the |
428 * Uses the embedded data if available, otherwise fetches the |
429 * data from the server. |
429 * data from the server. |
430 * |
430 * |
431 * @return {Deferred.promise} promise Resolves to a wp.api.collections[ collectionName ] |
431 * @return {Deferred.promise} promise Resolves to a wp.api.collections[ collectionName ] |
432 * collection. |
432 * collection. |
433 */ |
433 */ |
434 var postId, embeddeds, getObjects, |
434 var postId, embeddedObjects, getObjects, |
435 classProperties = '', |
435 classProperties = '', |
436 properties = '', |
436 properties = '', |
437 deferred = jQuery.Deferred(); |
437 deferred = jQuery.Deferred(); |
438 |
438 |
439 postId = parentModel.get( 'id' ); |
439 postId = parentModel.get( 'id' ); |
440 embeddeds = parentModel.get( '_embedded' ) || {}; |
440 embeddedObjects = parentModel.get( '_embedded' ) || {}; |
441 |
441 |
442 // Verify that we have a valid post ID. |
442 // Verify that we have a valid post ID. |
443 if ( ! _.isNumber( postId ) || 0 === postId ) { |
443 if ( ! _.isNumber( postId ) || 0 === postId ) { |
444 deferred.reject(); |
444 deferred.reject(); |
445 return deferred; |
445 return deferred; |
446 } |
446 } |
447 |
447 |
448 // If we have embedded getObjects data, use that when constructing the getObjects. |
448 // If we have embedded getObjects data, use that when constructing the getObjects. |
449 if ( ! _.isUndefined( embedSourcePoint ) && ! _.isUndefined( embeddeds[ embedSourcePoint ] ) ) { |
449 if ( ! _.isUndefined( embedSourcePoint ) && ! _.isUndefined( embeddedObjects[ embedSourcePoint ] ) ) { |
450 |
450 |
451 // Some embeds also include an index offset, check for that. |
451 // Some embeds also include an index offset, check for that. |
452 if ( _.isUndefined( embedIndex ) ) { |
452 if ( _.isUndefined( embedIndex ) ) { |
453 |
453 |
454 // Use the embed source point directly. |
454 // Use the embed source point directly. |
455 properties = embeddeds[ embedSourcePoint ]; |
455 properties = embeddedObjects[ embedSourcePoint ]; |
456 } else { |
456 } else { |
457 |
457 |
458 // Add the index to the embed source point. |
458 // Add the index to the embed source point. |
459 properties = embeddeds[ embedSourcePoint ][ embedIndex ]; |
459 properties = embeddedObjects[ embedSourcePoint ][ embedIndex ]; |
460 } |
460 } |
461 } else { |
461 } else { |
462 |
462 |
463 // Otherwise use the postId. |
463 // Otherwise use the postId. |
464 classProperties = { parent: postId }; |
464 classProperties = { parent: postId }; |
1410 loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( { |
1410 loadingObjects.collections[ collectionClassName ] = wp.api.WPApiBaseCollection.extend( { |
1411 |
1411 |
1412 // Function that returns a constructed url passed on the parent. |
1412 // Function that returns a constructed url passed on the parent. |
1413 url: function() { |
1413 url: function() { |
1414 return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) + |
1414 return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) + |
1415 parentName + '/' + this.parent + '/' + |
1415 parentName + '/' + |
1416 routeName; |
1416 ( ( _.isUndefined( this.parent ) || '' === this.parent ) ? |
|
1417 ( _.isUndefined( this.get( 'parent_post' ) ) ? '' : this.get( 'parent_post' ) + '/' ) : |
|
1418 this.parent + '/' ) + |
|
1419 routeName; |
1417 }, |
1420 }, |
1418 |
1421 |
1419 // Specify the model that this collection contains. |
1422 // Specify the model that this collection contains. |
1420 model: function( attrs, options ) { |
1423 model: function( attrs, options ) { |
1421 return new loadingObjects.models[ modelClassName ]( attrs, options ); |
1424 return new loadingObjects.models[ modelClassName ]( attrs, options ); |