mirror of
https://github.com/WordPress/WordPress.git
synced 2024-08-30 17:49:38 +02:00
* The way that the JS modules for media are currently set up turns the existing global `wp.media` namespace into a read-only API, this is bad. * For the existing module implementation to work with plugins, those looking to override or extend a class would have to modify their own plugin to use `browserify` - we can't expect this to happen * Because the general way that plugins override media classes is via machete (resetting them to something else), we cannot use `require( 'module' )` in the internal code for media modules We CAN continue to use `require( 'fun/js' )` in the manifests for media. Future code/projects should carefully consider what is made to be public API. In 3.5, EVERYTHING was made public, so everything shall remain public. See #31684, #28510. Built from https://develop.svn.wordpress.org/trunk@31935 git-svn-id: http://core.svn.wordpress.org/trunk@31914 1a063a9b-81f0-0310-95a4-ce76da25c4cd
169 lines
4.5 KiB
JavaScript
169 lines
4.5 KiB
JavaScript
/*globals wp, _, Backbone */
|
|
|
|
/**
|
|
* wp.media.model.Attachment
|
|
*
|
|
* @class
|
|
* @augments Backbone.Model
|
|
*/
|
|
var $ = Backbone.$,
|
|
Attachment;
|
|
|
|
Attachment = Backbone.Model.extend({
|
|
/**
|
|
* Triggered when attachment details change
|
|
* Overrides Backbone.Model.sync
|
|
*
|
|
* @param {string} method
|
|
* @param {wp.media.model.Attachment} model
|
|
* @param {Object} [options={}]
|
|
*
|
|
* @returns {Promise}
|
|
*/
|
|
sync: function( method, model, options ) {
|
|
// If the attachment does not yet have an `id`, return an instantly
|
|
// rejected promise. Otherwise, all of our requests will fail.
|
|
if ( _.isUndefined( this.id ) ) {
|
|
return $.Deferred().rejectWith( this ).promise();
|
|
}
|
|
|
|
// Overload the `read` request so Attachment.fetch() functions correctly.
|
|
if ( 'read' === method ) {
|
|
options = options || {};
|
|
options.context = this;
|
|
options.data = _.extend( options.data || {}, {
|
|
action: 'get-attachment',
|
|
id: this.id
|
|
});
|
|
return wp.media.ajax( options );
|
|
|
|
// Overload the `update` request so properties can be saved.
|
|
} else if ( 'update' === method ) {
|
|
// If we do not have the necessary nonce, fail immeditately.
|
|
if ( ! this.get('nonces') || ! this.get('nonces').update ) {
|
|
return $.Deferred().rejectWith( this ).promise();
|
|
}
|
|
|
|
options = options || {};
|
|
options.context = this;
|
|
|
|
// Set the action and ID.
|
|
options.data = _.extend( options.data || {}, {
|
|
action: 'save-attachment',
|
|
id: this.id,
|
|
nonce: this.get('nonces').update,
|
|
post_id: wp.media.model.settings.post.id
|
|
});
|
|
|
|
// Record the values of the changed attributes.
|
|
if ( model.hasChanged() ) {
|
|
options.data.changes = {};
|
|
|
|
_.each( model.changed, function( value, key ) {
|
|
options.data.changes[ key ] = this.get( key );
|
|
}, this );
|
|
}
|
|
|
|
return wp.media.ajax( options );
|
|
|
|
// Overload the `delete` request so attachments can be removed.
|
|
// This will permanently delete an attachment.
|
|
} else if ( 'delete' === method ) {
|
|
options = options || {};
|
|
|
|
if ( ! options.wait ) {
|
|
this.destroyed = true;
|
|
}
|
|
|
|
options.context = this;
|
|
options.data = _.extend( options.data || {}, {
|
|
action: 'delete-post',
|
|
id: this.id,
|
|
_wpnonce: this.get('nonces')['delete']
|
|
});
|
|
|
|
return wp.media.ajax( options ).done( function() {
|
|
this.destroyed = true;
|
|
}).fail( function() {
|
|
this.destroyed = false;
|
|
});
|
|
|
|
// Otherwise, fall back to `Backbone.sync()`.
|
|
} else {
|
|
/**
|
|
* Call `sync` directly on Backbone.Model
|
|
*/
|
|
return Backbone.Model.prototype.sync.apply( this, arguments );
|
|
}
|
|
},
|
|
/**
|
|
* Convert date strings into Date objects.
|
|
*
|
|
* @param {Object} resp The raw response object, typically returned by fetch()
|
|
* @returns {Object} The modified response object, which is the attributes hash
|
|
* to be set on the model.
|
|
*/
|
|
parse: function( resp ) {
|
|
if ( ! resp ) {
|
|
return resp;
|
|
}
|
|
|
|
resp.date = new Date( resp.date );
|
|
resp.modified = new Date( resp.modified );
|
|
return resp;
|
|
},
|
|
/**
|
|
* @param {Object} data The properties to be saved.
|
|
* @param {Object} options Sync options. e.g. patch, wait, success, error.
|
|
*
|
|
* @this Backbone.Model
|
|
*
|
|
* @returns {Promise}
|
|
*/
|
|
saveCompat: function( data, options ) {
|
|
var model = this;
|
|
|
|
// If we do not have the necessary nonce, fail immeditately.
|
|
if ( ! this.get('nonces') || ! this.get('nonces').update ) {
|
|
return $.Deferred().rejectWith( this ).promise();
|
|
}
|
|
|
|
return wp.media.post( 'save-attachment-compat', _.defaults({
|
|
id: this.id,
|
|
nonce: this.get('nonces').update,
|
|
post_id: wp.media.model.settings.post.id
|
|
}, data ) ).done( function( resp, status, xhr ) {
|
|
model.set( model.parse( resp, xhr ), options );
|
|
});
|
|
}
|
|
}, {
|
|
/**
|
|
* Create a new model on the static 'all' attachments collection and return it.
|
|
*
|
|
* @static
|
|
* @param {Object} attrs
|
|
* @returns {wp.media.model.Attachment}
|
|
*/
|
|
create: function( attrs ) {
|
|
var Attachments = wp.media.model.Attachments;
|
|
return Attachments.all.push( attrs );
|
|
},
|
|
/**
|
|
* Create a new model on the static 'all' attachments collection and return it.
|
|
*
|
|
* If this function has already been called for the id,
|
|
* it returns the specified attachment.
|
|
*
|
|
* @static
|
|
* @param {string} id A string used to identify a model.
|
|
* @param {Backbone.Model|undefined} attachment
|
|
* @returns {wp.media.model.Attachment}
|
|
*/
|
|
get: _.memoize( function( id, attachment ) {
|
|
var Attachments = wp.media.model.Attachments;
|
|
return Attachments.all.push( attachment || { id: id } );
|
|
})
|
|
});
|
|
|
|
module.exports = Attachment;
|