]> scripts.mit.edu Git - autoinstallsdev/wordpress.git/blobdiff - wp-includes/js/media-models.js
Wordpress 3.7
[autoinstallsdev/wordpress.git] / wp-includes / js / media-models.js
index 34cce0b7adc9e7355286b9c2488ef8a73366f44c..78489d0797d3341e7027f2f6d6ac0d48f94975c8 100644 (file)
@@ -72,90 +72,25 @@ window.wp = window.wp || {};
                 * 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 ) {
@@ -261,13 +196,12 @@ window.wp = window.wp || {};
                                });
 
                                // 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 );
@@ -292,6 +226,10 @@ window.wp = window.wp || {};
                                }).fail( function() {
                                        this.destroyed = false;
                                });
+
+                       // Otherwise, fall back to `Backbone.sync()`.
+                       } else {
+                               return Backbone.Model.prototype.sync.apply( this, arguments );
                        }
                },
 
@@ -392,7 +330,7 @@ window.wp = window.wp || {};
                        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 );
 
@@ -434,7 +372,7 @@ window.wp = window.wp || {};
 
                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 );
@@ -463,7 +401,6 @@ window.wp = window.wp || {};
 
                        attachments.on( 'add change remove', this._validateHandler, this );
                        attachments.on( 'reset', this._validateAllHandler, this );
-
                        this.validateAll( attachments );
                        return this;
                },
@@ -545,9 +482,26 @@ window.wp = window.wp || {};
                },
 
                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;
                        });
                },
 
@@ -718,7 +672,7 @@ window.wp = window.wp || {};
                                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 )
@@ -727,7 +681,7 @@ window.wp = window.wp || {};
                },
 
                sync: function( method, model, options ) {
-                       var fallback;
+                       var args, fallback;
 
                        // Overload the read method so Attachment.fetch() functions correctly.
                        if ( 'read' === method ) {
@@ -876,7 +830,7 @@ window.wp = window.wp || {};
                                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();
@@ -888,7 +842,7 @@ window.wp = window.wp || {};
 
                                        // 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 )