| 1 |
const StockpackImageModel = Backbone.Model.extend( /** @lends wp.media.model.Attachment.prototype */{ |
| 2 |
/** |
| 3 |
* Triggered when attachment details change |
| 4 |
* Overrides Backbone.Model.sync |
| 5 |
* |
| 6 |
* @param {string} method |
| 7 |
* @param {wp.media.model.Attachment} model |
| 8 |
* @param {Object} [options={}] |
| 9 |
* |
| 10 |
* @return {Promise} response |
| 11 |
*/ |
| 12 |
sync( method, model, options ) { |
| 13 |
// If the attachment does not yet have an `id`, return an instantly |
| 14 |
// rejected promise. Otherwise, all of our requests will fail. |
| 15 |
if ( _.isUndefined( this.id ) ) { |
| 16 |
return $.Deferred().rejectWith( this ).promise(); |
| 17 |
} |
| 18 |
|
| 19 |
// Overload the `read` request so Attachment.fetch() functions correctly. |
| 20 |
if ( 'read' === method ) { |
| 21 |
options = options || {}; |
| 22 |
options.context = this; |
| 23 |
options.data = _.extend( options.data || {}, { |
| 24 |
action: 'get-stockpack-image', |
| 25 |
id: this.id, |
| 26 |
} ); |
| 27 |
return wp.media.ajax( options ); |
| 28 |
|
| 29 |
// Overload the `update` request so properties can be saved. |
| 30 |
} else if ( 'update' === method ) { |
| 31 |
// If we do not have the necessary nonce, fail immeditately. |
| 32 |
if ( ! this.get( 'nonces' ) || ! this.get( 'nonces' ).update ) { |
| 33 |
return $.Deferred().rejectWith( this ).promise(); |
| 34 |
} |
| 35 |
|
| 36 |
options = options || {}; |
| 37 |
options.context = this; |
| 38 |
|
| 39 |
// Set the action and ID. |
| 40 |
options.data = _.extend( options.data || {}, { |
| 41 |
action: 'save-attachment', |
| 42 |
id: this.id, |
| 43 |
nonce: this.get( 'nonces' ).update, |
| 44 |
post_id: wp.media.model.settings.post.id, |
| 45 |
} ); |
| 46 |
|
| 47 |
// Record the values of the changed attributes. |
| 48 |
if ( model.hasChanged() ) { |
| 49 |
options.data.changes = {}; |
| 50 |
|
| 51 |
_.each( model.changed, function( value, key ) { |
| 52 |
options.data.changes[ key ] = this.get( key ); |
| 53 |
}, this ); |
| 54 |
} |
| 55 |
|
| 56 |
return wp.media.ajax( options ); |
| 57 |
|
| 58 |
// Overload the `delete` request so attachments can be removed. |
| 59 |
// This will permanently delete an attachment. |
| 60 |
} |
| 61 |
/** |
| 62 |
* Call `sync` directly on Backbone.Model |
| 63 |
*/ |
| 64 |
return Backbone.Model.prototype.sync.apply( this, arguments ); |
| 65 |
}, |
| 66 |
|
| 67 |
progress() { |
| 68 |
if ( this.$bar && this.$bar.length ) { |
| 69 |
this.$bar.width( this.model.get( 'percent' ) + '%' ); |
| 70 |
} |
| 71 |
}, |
| 72 |
|
| 73 |
}, { |
| 74 |
/** |
| 75 |
* Create a new model on the static 'all' attachments collection and return it. |
| 76 |
* |
| 77 |
* @static |
| 78 |
* |
| 79 |
* @param {Object} attrs |
| 80 |
* @return {StockpackImageModel} model |
| 81 |
*/ |
| 82 |
create( attrs ) { |
| 83 |
const Images = StockpackImageModel; |
| 84 |
return Images.all.push( attrs ); |
| 85 |
}, |
| 86 |
/** |
| 87 |
* Create a new model on the static 'all' attachments collection and return it. |
| 88 |
* |
| 89 |
* If this function has already been called for the id, |
| 90 |
* it returns the specified attachment. |
| 91 |
* |
| 92 |
* @static |
| 93 |
* @param {string} id A string used to identify a model. |
| 94 |
* @param {Backbone.Model|undefined} attachment |
| 95 |
* @return {StockpackImageModel} |
| 96 |
*/ |
| 97 |
get: _.memoize( function( id, attachment ) { |
| 98 |
const Images = StockpackImageModel; |
| 99 |
return Images.all.push( attachment || { id } ); |
| 100 |
} ), |
| 101 |
} ); |
| 102 |
|
| 103 |
export default StockpackImageModel; |
| 104 |
|