Make WordPress Core

Changeset 22655


Ignore:
Timestamp:
11/19/2012 02:43:10 AM (11 years ago)
Author:
koopersmith
Message:

Media: Optimize media models.

  • Merge Composite model with general Attachments model, as Attachments.validate and Composite.evaluate were functionally equivalent.
  • Queries should only watch wp.Uploader.queue, as watching Attachments.all results in queries attempting to add attachments before their properties are set (which then results a few too many irrelevant adds/removes). Attachments.all should potentially be removed or rethought.

see #21390.

Location:
trunk/wp-includes/js
Files:
2 edited

Legend:

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

    r22654 r22655  
    359359
    360360        validate: function( attachment, options ) {
     361            var valid = this.validator( attachment ),
     362                hasAttachment = !! this.getByCid( attachment.cid );
     363
    361364            // Only retain the `silent` option.
    362365            options = {
     
    364367            };
    365368
    366             return this[ this.validator( attachment ) ? 'add' : 'remove' ]( attachment, options );
     369            if ( ! valid && hasAttachment )
     370                this.remove( attachment, options );
     371            else if ( valid && ! hasAttachment )
     372                this.add( attachment, options );
     373
     374            return this;
     375        },
     376
     377        validateAll: function( attachments ) {
     378            _.each( attachments.models, function( attachment ) {
     379                this.validate( attachment, { silent: true });
     380            }, this );
     381
     382            return this;
    367383        },
    368384
    369385        observe: function( attachments ) {
    370             attachments.on( 'add change', this.validate, this );
     386            this.observers = this.observers || [];
     387            this.observers.push( attachments );
     388
     389            attachments.on( 'add change remove', this._validateHandler, this );
     390            attachments.on( 'reset', this._validateAllHandler, this );
     391
     392            this.validateAll( attachments );
     393            return this;
    371394        },
    372395
    373396        unobserve: function( attachments ) {
    374             attachments.off( 'add change', this.validate, this );
     397            if ( attachments ) {
     398                attachments.off( null, null, this );
     399                this.observers = _.without( this.observers, attachments );
     400
     401            } else {
     402                _.each( this.observers, function( attachments ) {
     403                    attachments.off( null, null, this );
     404                }, this );
     405                delete this.observers;
     406            }
     407
     408            return this;
     409        },
     410
     411        _validateHandler: function( attachment, attachments, options ) {
     412            return this.validate( attachment, options );
     413        },
     414
     415        _validateAllHandler: function( attachments, options ) {
     416            return this.evaluateAll( attachments, options );
    375417        },
    376418
     
    519561            };
    520562
    521             // Observe the central `Attachments.all` model to watch for new
    522             // matches for the query.
     563            // Observe the central `wp.Uploader.queue` collection to watch for
     564            // new matches for the query.
    523565            //
    524566            // Only observe when a limited number of query args are set. There
     
    526568            // false positives in those queries.
    527569            allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type' ];
    528             if ( _( this.args ).chain().keys().difference( allowed ).isEmpty().value() )
    529                 this.observe( Attachments.all );
     570            if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() )
     571                this.observe( wp.Uploader.queue );
    530572        },
    531573
     
    733775    });
    734776
    735     /**
    736      * wp.media.model.Composite
    737      *
    738      * Creates a model that can simultaneously pull from two or more collections.
    739      */
    740     media.model.Composite = Attachments.extend({
    741         initialize: function( models, options ) {
    742             this.observe( this, { silent: true });
    743             Attachments.prototype.initialize.apply( this, arguments );
    744         },
    745 
    746         evaluate: function( attachment, options ) {
    747             var valid = this.validator( attachment ),
    748                 hasAttachment = !! this.getByCid( attachment.cid );
    749 
    750             if ( ! valid && hasAttachment )
    751                 this.remove( attachment, options );
    752             else if ( valid && ! hasAttachment )
    753                 this.add( attachment, options );
    754 
    755             return this;
    756         },
    757 
    758         validator: function() {
    759             return true;
    760         },
    761 
    762         evaluateAll: function( attachments, options ) {
    763             _.each( attachments.models, function( attachment ) {
    764                 this.evaluate( attachment, { silent: true });
    765             }, this );
    766 
    767             return this;
    768         },
    769 
    770         observe: function( attachments, options ) {
    771             var silent = options && options.silent;
    772             this.observers = this.observers || [];
    773             this.observers.push( attachments );
    774 
    775             attachments.on( 'add remove',  silent ? this._evaluateSilentHandler : this._evaluateHandler, this );
    776             attachments.on( 'reset',  silent ? this._evaluateAllSilentHandler : this._evaluateAllHandler, this );
    777 
    778             this.evaluateAll( attachments, options );
    779             return this;
    780         },
    781 
    782         unobserve: function( attachments ) {
    783             if ( attachments ) {
    784                 attachments.off( null, null, this );
    785                 this.observers = _.without( this.observers, attachments );
    786 
    787             } else {
    788                 _.each( this.observers, function( attachments ) {
    789                     attachments.off( null, null, this );
    790                 }, this );
    791                 delete this.observers;
    792             }
    793 
    794             return this;
    795         },
    796 
    797         _evaluateHandler: function( attachment, attachments, options ) {
    798             return this.evaluate( attachment, options );
    799         },
    800 
    801         _evaluateAllHandler: function( attachments, options ) {
    802             return this.evaluateAll( attachments, options );
    803         },
    804 
    805         _evaluateSilentHandler: function( attachment, attachments, options ) {
    806             return this.evaluate( attachment, _.defaults({ silent: true }, options ) );
    807         },
    808 
    809         _evaluateAllSilentHandler: function( attachments, options ) {
    810             return this.evaluateAll( attachments, _.defaults({ silent: true }, options ) );
    811         }
    812     });
    813 
    814777}(jQuery));
  • trunk/wp-includes/js/media-views.js

    r22651 r22655  
    388388
    389389            // Create a composite library in its place.
    390             composite = new media.model.Composite( null, {
     390            composite = new media.model.Attachments( null, {
    391391                props: _.pick( original.props.toJSON(), 'order', 'orderby' )
    392392            });
     
    24112411            _.each(['add','remove'], function( method ) {
    24122412                this.collection.on( method, function( attachment, attachments, options ) {
     2413                    console.log( method, 'attachment', attachment.id, 'at', options.index );
    24132414                    this[ method ]( attachment, options.index );
    24142415                }, this );
Note: See TracChangeset for help on using the changeset viewer.