* media.template( id )
*
* Fetches a template by id.
- *
- * @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-".
- * For example, "attachment" maps to "tmpl-attachment".
- * @return {function} A function that lazily-compiles the template requested.
+ * See wp.template() in `wp-includes/js/wp-util.js`.
*/
- template: _.memoize( function( id ) {
- var compiled,
- options = {
- evaluate: /<#([\s\S]+?)#>/g,
- interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
- escape: /\{\{([^\}]+?)\}\}(?!\})/g,
- variable: 'data'
- };
-
- return function( data ) {
- compiled = compiled || _.template( $( '#tmpl-' + id ).html(), null, options );
- return compiled( data );
- };
- }),
+ template: wp.template,
/**
* media.post( [action], [data] )
*
* Sends a POST request to WordPress.
- *
- * @param {string} action The slug of the action to fire in WordPress.
- * @param {object} data The data to populate $_POST with.
- * @return {$.promise} A jQuery promise that represents the request.
+ * See wp.ajax.post() in `wp-includes/js/wp-util.js`.
*/
- post: function( action, data ) {
- return media.ajax({
- data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
- });
- },
+ post: wp.ajax.post,
/**
* media.ajax( [action], [options] )
*
- * Sends a POST request to WordPress.
- *
- * @param {string} action The slug of the action to fire in WordPress.
- * @param {object} options The options passed to jQuery.ajax.
- * @return {$.promise} A jQuery promise that represents the request.
+ * Sends an XHR request to WordPress.
+ * See wp.ajax.send() in `wp-includes/js/wp-util.js`.
*/
- ajax: function( action, options ) {
- if ( _.isObject( action ) ) {
- options = action;
- } else {
- options = options || {};
- options.data = _.extend( options.data || {}, { action: action });
- }
-
- options = _.defaults( options || {}, {
- type: 'POST',
- url: media.model.settings.ajaxurl,
- context: this
- });
-
- return $.Deferred( function( deferred ) {
- // Transfer success/error callbacks.
- if ( options.success )
- deferred.done( options.success );
- if ( options.error )
- deferred.fail( options.error );
-
- delete options.success;
- delete options.error;
-
- // Use with PHP's wp_send_json_success() and wp_send_json_error()
- $.ajax( options ).done( function( response ) {
- // Treat a response of `1` as successful for backwards
- // compatibility with existing handlers.
- if ( response === '1' || response === 1 )
- response = { success: true };
-
- if ( _.isObject( response ) && ! _.isUndefined( response.success ) )
- deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
- else
- deferred.rejectWith( this, [response] );
- }).fail( function() {
- deferred.rejectWith( this, arguments );
- });
- }).promise();
- },
+ ajax: wp.ajax.send,
// Scales a set of dimensions to fit within bounding dimensions.
fit: function( dimensions ) {
});
// Record the values of the changed attributes.
- if ( options.changes ) {
- _.each( options.changes, function( value, key ) {
- options.changes[ key ] = this.get( key );
- }, this );
+ if ( model.hasChanged() ) {
+ options.data.changes = {};
- options.data.changes = options.changes;
- delete options.changes;
+ _.each( model.changed, function( value, key ) {
+ options.data.changes[ key ] = this.get( key );
+ }, this );
}
return media.ajax( options );
}).fail( function() {
this.destroyed = false;
});
+
+ // Otherwise, fall back to `Backbone.sync()`.
+ } else {
+ return Backbone.Model.prototype.sync.apply( this, arguments );
}
},
if ( this.props.get('query') )
return;
- var changed = _.chain( options.changes ).map( function( t, prop ) {
+ var changed = _.chain( model.changed ).map( function( t, prop ) {
var filter = Attachments.filters[ prop ],
term = model.get( prop );
validate: function( attachment, options ) {
var valid = this.validator( attachment ),
- hasAttachment = !! this.getByCid( attachment.cid );
+ hasAttachment = !! this.get( attachment.cid );
if ( ! valid && hasAttachment )
this.remove( attachment, options );
attachments.on( 'add change remove', this._validateHandler, this );
attachments.on( 'reset', this._validateAllHandler, this );
-
this.validateAll( attachments );
return this;
},
},
parse: function( resp, xhr ) {
+ if ( ! _.isArray( resp ) )
+ resp = [resp];
+
return _.map( resp, function( attrs ) {
- var attachment = Attachment.get( attrs.id );
- return attachment.set( attachment.parse( attrs, xhr ) );
+ var id, attachment, newAttributes;
+
+ if ( attrs instanceof Backbone.Model ) {
+ id = attrs.get( 'id' );
+ attrs = attrs.attributes;
+ } else {
+ id = attrs.id;
+ }
+
+ attachment = Attachment.get( id );
+ newAttributes = attachment.parse( attrs, xhr );
+
+ if ( ! _.isEqual( attachment.attributes, newAttributes ) )
+ attachment.set( newAttributes );
+
+ return attachment;
});
},
return $.Deferred().resolveWith( this ).promise();
options = options || {};
- options.add = true;
+ options.remove = false;
return this._more = this.fetch( options ).done( function( resp ) {
if ( _.isEmpty( resp ) || -1 === this.args.posts_per_page || resp.length < this.args.posts_per_page )
},
sync: function( method, model, options ) {
- var fallback;
+ var args, fallback;
// Overload the read method so Attachment.fetch() functions correctly.
if ( 'read' === method ) {
this._single = model;
// If the single model isn't in the selection, remove it.
- if ( this._single && ! this.getByCid( this._single.cid ) )
+ if ( this._single && ! this.get( this._single.cid ) )
delete this._single;
this._single = this._single || this.last();
// If the model was already removed, trigger the collection
// event manually.
- if ( ! this.getByCid( previous.cid ) )
+ if ( ! this.get( previous.cid ) )
this.trigger( 'selection:unsingle', previous, this );
}
if ( this._single )