Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Sep 5, 2018. It is now read-only.
/client-jsPublic archive

Promote buildModelGetter & buildCollectionGetter from private functions to wp.api.utils#157

Open
westonruter wants to merge2 commits intoWP-API:master
base:master
Choose a base branch
Loading
fromxwp:feature/public-getter-builders
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
321 changes: 160 additions & 161 deletionsjs/utils.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -189,11 +189,158 @@

};

/**
* Build a helper function to retrieve related model.
*
* @param {Backbone.Model} parentModel - The parent model.
* @param {int} modelId - The model ID if the object to request
* @param {string} modelName - The model name to use when constructing the model.
* @param {string} embedSourcePoint - Where to check the embeds object for _embed data.
* @param {string} embedCheckField - Which model field to check to see if the model has data.
*
* @return {Deferred.promise} A promise which resolves to the constructed model.
*/
wp.api.utils.buildModelGetter = function buildModelGetter( parentModel, modelId, modelName, embedSourcePoint, embedCheckField ) {
var getModel, embeddeds, attributes, deferred;

deferred = jQuery.Deferred();
embeddeds = parentModel.get( '_embedded' ) || {};

// Verify that we have a valid object id.
if ( ! _.isNumber( modelId ) || 0 === modelId ) {
deferred.reject();
return deferred;
}

// If we have embedded object data, use that when constructing the getModel.
if ( embeddeds[ embedSourcePoint ] ) {
attributes = _.findWhere( embeddeds[ embedSourcePoint ], { id: modelId } );
}

// Otherwise use the modelId.
if ( ! attributes ) {
attributes = { id: modelId };
}

// Create the new getModel model.
getModel = new wp.api.models[ modelName ]( attributes );

// If we didn’t have an embedded getModel, fetch the getModel data.
if ( ! getModel.get( embedCheckField ) ) {
getModel.fetch( {
success: function( getModel ) {
deferred.resolve( getModel );
},
error: function( getModel, response ) {
deferred.reject( response );
}
} );
} else {

// Resolve with the embedded model.
deferred.resolve( getModel );
}

// Return a promise.
return deferred.promise();
};

/**
* Build a helper to retrieve a collection.
*
* @param {Backbone.Model} parentModel - The parent model.
* @param {string} collectionName - The name to use when constructing the collection.
* @param {string} [embedSourcePoint] - Where to check the embeds object for _embed data.
* @param {string} [embedIndex] - An additional optional index for the _embed data.
*
* @return {Deferred.promise} A promise which resolves to the constructed collection.
*/
wp.api.utils.buildCollectionGetter = function buildCollectionGetter( parentModel, collectionName, embedSourcePoint, embedIndex ) {
/**
* Returns a promise that resolves to the requested collection
*
* Uses the embedded data if available, otherwise fetches the
* data from the server.
*
* @return {Deferred.promise} promise Resolves to a wp.api.collections[ collectionName ] collection.
*/
var postId, embeddeds, getObjects, setHelperParentPost,
classProperties = '',
properties = '',
deferred = jQuery.Deferred();

postId = parentModel.get( 'id' );
embeddeds = parentModel.get( '_embedded' ) || {};

// Verify that we have a valid post id.
if ( ! _.isNumber( postId ) || 0 === postId ) {
deferred.reject();
return deferred;
}

/**
* Set the model post parent.
*/
setHelperParentPost = function( collection, postId ) {

// Attach post_parent id to the collection.
_.each( collection.models, function( model ) {
model.set( 'parent_post', postId );
} );
};

// If we have embedded getObjects data, use that when constructing the getObjects.
if ( ! _.isUndefined( embedSourcePoint ) && ! _.isUndefined( embeddeds[ embedSourcePoint ] ) ) {

// Some embeds also include an index offset, check for that.
if ( _.isUndefined( embedIndex ) ) {

// Use the embed source point directly.
properties = embeddeds[ embedSourcePoint ];
} else {

// Add the index to the embed source point.
properties = embeddeds[ embedSourcePoint ][ embedIndex ];
}
} else {

// Otherwise use the postId.
classProperties = { parent: postId };
}

// Create the new getObjects collection.
getObjects = new wp.api.collections[ collectionName ]( properties, classProperties );

// If we didn’t have embedded getObjects, fetch the getObjects data.
if ( _.isUndefined( getObjects.models[0] ) ) {
getObjects.fetch( {
success: function( getObjects ) {

// Add a helper 'parent_post' attribute onto the model.
setHelperParentPost( getObjects, postId );
deferred.resolve( getObjects );
},
error: function( getModel, response ) {
deferred.reject( response );
}
} );
} else {

// Add a helper 'parent_post' attribute onto the model.
setHelperParentPost( getObjects, postId );
deferred.resolve( getObjects );
}

// Return a promise.
return deferred.promise();

};

/**
* Add mixins and helpers to models depending on their defaults.
*
* @param {BackboneModel} model The model to attach helpers and mixins to.
* @param {string} modelClassName Theclassname of the constructed model.
* @param {Backbone.Model} model The model to attach helpers and mixins to.
* @param {string} modelClassName Theclass name of the constructed model.
* @param {Object} loadingObjects An object containing the models and collections we are building.
*/
wp.api.utils.addMixinsAndHelpers = function( model, modelClassName, loadingObjects ) {
Expand All@@ -214,7 +361,7 @@
* to or from the server. For example, a date stored as `2015-12-27T21:22:24` on the server
* gets expanded to `Sun Dec 27 2015 14:22:24 GMT-0700 (MST)` when the model is fetched.
*
* @type {{toJSON: toJSON, parse: parse}}.
* @type {{setDate: setDate, getDate: getDate}}.
*/
TimeStampedMixin = {

Expand All@@ -228,7 +375,7 @@
* @param {string} field The date field to set. One of 'date', 'date_gmt', 'date_modified'
* or 'date_modified_gmt'. Optional, defaults to 'date'.
*/
setDate: function( date, field ) {
setDate: function setDate( date, field ) {
var theField = field || 'date';

// Don't alter non parsable date fields.
Expand All@@ -248,7 +395,7 @@
* @param {string} field The date field to set. One of 'date', 'date_gmt', 'date_modified'
* or 'date_modified_gmt'. Optional, defaults to 'date'.
*/
getDate: function( field ) {
getDate: function getDate( field ) {
var theField = field || 'date',
theISODate = this.get( theField );

Expand All@@ -262,173 +409,25 @@
},

/**
* Build a helper function to retrieve related model.
*
* @param {string} parentModel The parent model.
* @param {int} modelId The model ID if the object to request
* @param {string} modelName The model name to use when constructing the model.
* @param {string} embedSourcePoint Where to check the embedds object for _embed data.
* @param {string} embedCheckField Which model field to check to see if the model has data.
*
* @return {Deferred.promise} A promise which resolves to the constructed model.
*/
buildModelGetter = function( parentModel, modelId, modelName, embedSourcePoint, embedCheckField ) {
var getModel, embeddeds, attributes, deferred;

deferred = jQuery.Deferred();
embeddeds = parentModel.get( '_embedded' ) || {};

// Verify that we have a valied object id.
if ( ! _.isNumber( modelId ) || 0 === modelId ) {
deferred.reject();
return deferred;
}

// If we have embedded object data, use that when constructing the getModel.
if ( embeddeds[ embedSourcePoint ] ) {
attributes = _.findWhere( embeddeds[ embedSourcePoint ], { id: modelId } );
}

// Otherwise use the modelId.
if ( ! attributes ) {
attributes = { id: modelId };
}

// Create the new getModel model.
getModel = new wp.api.models[ modelName ]( attributes );

// If we didn’t have an embedded getModel, fetch the getModel data.
if ( ! getModel.get( embedCheckField ) ) {
getModel.fetch( {
success: function( getModel ) {
deferred.resolve( getModel );
},
error: function( getModel, response ) {
deferred.reject( response );
}
} );
} else {

// Resolve with the embedded model.
deferred.resolve( getModel );
}

// Return a promise.
return deferred.promise();
},

/**
* Build a helper to retrieve a collection.
*
* @param {string} parentModel The parent model.
* @param {string} collectionName The name to use when constructing the collection.
* @param {string} embedSourcePoint Where to check the embedds object for _embed data.
* @param {string} embedIndex An addiitonal optional index for the _embed data.
*
* @return {Deferred.promise} A promise which resolves to the constructed collection.
*/
buildCollectionGetter = function( parentModel, collectionName, embedSourcePoint, embedIndex ) {
/**
* Returns a promise that resolves to the requested collection
*
* Uses the embedded data if available, otherwises fetches the
* data from the server.
*
* @return {Deferred.promise} promise Resolves to a wp.api.collections[ collectionName ]
* collection.
*/
var postId, embeddeds, getObjects,
classProperties = '',
properties = '',
deferred = jQuery.Deferred();

postId = parentModel.get( 'id' );
embeddeds = parentModel.get( '_embedded' ) || {};

// Verify that we have a valied post id.
if ( ! _.isNumber( postId ) || 0 === postId ) {
deferred.reject();
return deferred;
}

// If we have embedded getObjects data, use that when constructing the getObjects.
if ( ! _.isUndefined( embedSourcePoint ) && ! _.isUndefined( embeddeds[ embedSourcePoint ] ) ) {

// Some embeds also include an index offset, check for that.
if ( _.isUndefined( embedIndex ) ) {

// Use the embed source point directly.
properties = embeddeds[ embedSourcePoint ];
} else {

// Add the index to the embed source point.
properties = embeddeds[ embedSourcePoint ][ embedIndex ];
}
} else {

// Otherwise use the postId.
classProperties = { parent: postId };
}

// Create the new getObjects collection.
getObjects = new wp.api.collections[ collectionName ]( properties, classProperties );

// If we didn’t have embedded getObjects, fetch the getObjects data.
if ( _.isUndefined( getObjects.models[0] ) ) {
getObjects.fetch( {
success: function( getObjects ) {

// Add a helper 'parent_post' attribute onto the model.
setHelperParentPost( getObjects, postId );
deferred.resolve( getObjects );
},
error: function( getModel, response ) {
deferred.reject( response );
}
} );
} else {

// Add a helper 'parent_post' attribute onto the model.
setHelperParentPost( getObjects, postId );
deferred.resolve( getObjects );
}

// Return a promise.
return deferred.promise();

},

/**
* Set the model post parent.
*/
setHelperParentPost = function( collection, postId ) {

// Attach post_parent id to the collection.
_.each( collection.models, function( model ) {
model.set( 'parent_post', postId );
} );
},

/**
* Add a helper funtion to handle post Meta.
* Add a helper function to handle post Meta.
*/
MetaMixin = {
getMeta: function() {
return buildCollectionGetter( this, 'PostMeta', 'https://api.w.org/meta' );
returnwp.api.utils.buildCollectionGetter( this, 'PostMeta', 'https://api.w.org/meta' );
}
},

/**
* Add a helperfuntion to handle post Revisions.
* Add a helperfunction to handle post Revisions.
*/
RevisionsMixin = {
getRevisions: function() {
return buildCollectionGetter( this, 'PostRevisions' );
returnwp.api.utils.buildCollectionGetter( this, 'PostRevisions' );
}
},

/**
* Add a helperfuntion to handle post Tags.
* Add a helperfunction to handle post Tags.
*/
TagsMixin = {

Expand DownExpand Up@@ -512,7 +511,7 @@
},

/**
* Add a helperfuntion to handle post Categories.
* Add a helperfunction to handle post Categories.
*/
CategoriesMixin = {

Expand DownExpand Up@@ -601,7 +600,7 @@
*/
AuthorMixin = {
getAuthorUser: function() {
return buildModelGetter( this, this.get( 'author' ), 'User', 'author', 'name' );
returnwp.api.utils.buildModelGetter( this, this.get( 'author' ), 'User', 'author', 'name' );
}
},

Expand All@@ -610,7 +609,7 @@
*/
FeaturedMediaMixin = {
getFeaturedMedia: function() {
return buildModelGetter( this, this.get( 'featured_media' ), 'Media', 'wp:featuredmedia', 'source_url' );
returnwp.api.utils.buildModelGetter( this, this.get( 'featured_media' ), 'Media', 'wp:featuredmedia', 'source_url' );
}
};

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp