Source: media/models/attachment.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var $ = Backbone.$,
    Attachment;

/**
 * wp.media.model.Attachment
 *
 * @memberOf wp.media.model
 *
 * @class
 * @augments Backbone.Model
 */
Attachment = Backbone.Model.extend(/** @lends wp.media.model.Attachment.prototype */{
    /**
     * Triggered when attachment details change
     * Overrides Backbone.Model.sync
     *
     * @param {string} method
     * @param {wp.media.model.Attachment} model
     * @param {Object} [options={}]
     *
     * @return {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 immediately.
            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()
     * @return {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
     *
     * @return {Promise}
     */
    saveCompat: function( data, options ) {
        var model = this;

        // If we do not have the necessary nonce, fail immediately.
        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 );
        });
    }
},/** @lends wp.media.model.Attachment */{
    /**
     * Create a new model on the static 'all' attachments collection and return it.
     *
     * @static
     *
     * @param {Object} attrs
     * @return {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
     * @return {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;