Make WordPress Core

Changeset 24771


Ignore:
Timestamp:
07/22/2013 10:24:33 PM (13 years ago)
Author:
markjaquith
Message:

Fix a media regression in [24110] that could cause duplicate models.

Props garyc40. Fixes #24753. See #24094, #23830.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/media-models.js

    r24652 r24771  
    22
    33(function($){
    4         var Attachment, Attachments, Query, compare, l10n, media, bindSyncEvents;
     4        var Attachment, Attachments, Query, compare, l10n, media;
    55
    66        /**
     
    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                /**
     
    198177                                        id: this.id
    199178                                });
    200                                 bindSyncEvents( model, options );
    201179                                return media.ajax( options );
    202180
     
    227205                                }
    228206
    229                                 bindSyncEvents( model, options );
    230207                                return media.ajax( options );
    231208
     
    245222                                });
    246223
    247                                 bindSyncEvents( model, options );
    248224                                return media.ajax( options ).done( function() {
    249225                                        this.destroyed = true;
     
    505481                hasMore: function() {
    506482                        return this.mirroring ? this.mirroring.hasMore() : false;
     483                },
     484
     485                parse: function( resp, xhr ) {
     486                        if ( ! _.isArray( resp ) )
     487                                resp = [resp];
     488
     489                        return _.map( resp, function( attrs ) {
     490                                var id, attachment;
     491                                if ( attrs instanceof Backbone.Model ) {
     492                                        id = attrs.get( 'id' );
     493                                        attrs = attrs.attributes;
     494                                } else {
     495                                        id = attrs.id;
     496                                }
     497
     498                                attachment = Attachment.get( attrs.id );
     499                                if ( ! _.isEqual( attachment.attributes, attrs ) )
     500                                        attachment.set( attachment.parse( attrs, xhr ) );
     501
     502                                return attachment;
     503                        });
    507504                },
    508505
     
    702699
    703700                                options.data.query = args;
    704                                 bindSyncEvents( model, options );
    705701                                return media.ajax( options );
    706702
Note: See TracChangeset for help on using the changeset viewer.