Make WordPress Core

Ticket #24753: 24753.diff

File 24753.diff, 2.6 KB (added by garyc40, 12 years ago)
  • wp-includes/js/media-models.js

     
    11window.wp = window.wp || {};
    22
    33(function($){
    4         var Attachment, Attachments, Query, compare, l10n, media, bindSyncEvents;
     4        var Attachment, Attachments, Query, compare, l10n, media;
    55
    66        /**
    77         * wp.media( attributes )
    window.wp = window.wp || {}; 
    6767                        return a > b ? -1 : 1;
    6868        };
    6969
    70         // Ensures the 'sync' and 'error' events are always
    71         // correctly triggered when overloading `Backbone.sync`.
    72         bindSyncEvents = function( model, options ) {
    73                 var success = options.success,
    74                         error = options.error;
    75 
    76                 options.success = function( resp ) {
    77                         if ( success )
    78                                 success( resp );
    79                         model.trigger( 'sync', model, resp, options );
    80                 };
    81 
    82                 options.error = function( xhr ) {
    83                         if ( error )
    84                                 error( xhr );
    85                         model.trigger( 'error', model, xhr, options );
    86                 };
    87 
    88                 return options;
    89         };
    90 
    9170        _.extend( media, {
    9271                /**
    9372                 * media.template( id )
    window.wp = window.wp || {}; 
    197176                                        action: 'get-attachment',
    198177                                        id: this.id
    199178                                });
    200                                 bindSyncEvents( model, options );
    201179                                return media.ajax( options );
    202180
    203181                        // Overload the `update` request so properties can be saved.
    window.wp = window.wp || {}; 
    226204                                        }, this );
    227205                                }
    228206
    229                                 bindSyncEvents( model, options );
    230207                                return media.ajax( options );
    231208
    232209                        // Overload the `delete` request so attachments can be removed.
    window.wp = window.wp || {}; 
    244221                                        _wpnonce: this.get('nonces')['delete']
    245222                                });
    246223
    247                                 bindSyncEvents( model, options );
    248224                                return media.ajax( options ).done( function() {
    249225                                        this.destroyed = true;
    250226                                }).fail( function() {
    window.wp = window.wp || {}; 
    506482                        return this.mirroring ? this.mirroring.hasMore() : false;
    507483                },
    508484
     485                parse: function( resp, xhr ) {
     486                        if (! _.isArray(resp))
     487                                resp = [resp];
     488
     489                        return _.map( resp, function( attrs ) {
     490                                if (attrs instanceof Backbone.Model )
     491                                        return attrs;
     492
     493                                var attachment = Attachment.get( attrs.id );
     494                                return attachment.set( attachment.parse( attrs, xhr ) );
     495                        });
     496                },
     497
    509498                _requery: function() {
    510499                        if ( this.props.get('query') )
    511500                                this.mirror( Query.get( this.props.toJSON() ) );
    window.wp = window.wp || {}; 
    701690                                        args.paged = Math.floor( this.length / args.posts_per_page ) + 1;
    702691
    703692                                options.data.query = args;
    704                                 bindSyncEvents( model, options );
    705693                                return media.ajax( options );
    706694
    707695                        // Otherwise, fall back to Backbone.sync()