From ff1707892d8eaf73d6f919e91d5312b31e5c7ae2 Mon Sep 17 00:00:00 2001 From: Daryl Koopersmith Date: Wed, 21 Nov 2012 11:35:30 +0000 Subject: [PATCH] Media: Make mirroring a collection of attachments a special case of observing a collection of attachments. see #21390. git-svn-id: http://core.svn.wordpress.org/trunk@22746 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/js/media-models.js | 43 ++++++++++++++-------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/wp-includes/js/media-models.js b/wp-includes/js/media-models.js index b673e87845..925da4970c 100644 --- a/wp-includes/js/media-models.js +++ b/wp-includes/js/media-models.js @@ -362,11 +362,6 @@ window.wp = window.wp || {}; var valid = this.validator( attachment ), hasAttachment = !! this.getByCid( attachment.cid ); - // Only retain the `silent` option. - options = { - silent: options && options.silent - }; - if ( ! valid && hasAttachment ) this.remove( attachment, options ); else if ( valid && ! hasAttachment ) @@ -375,11 +370,16 @@ window.wp = window.wp || {}; return this; }, - validateAll: function( attachments ) { + validateAll: function( attachments, options ) { + options = options || {}; + _.each( attachments.models, function( attachment ) { this.validate( attachment, { silent: true }); }, this ); + if ( ! options.silent ) + this.trigger( 'reset', this, options ); + return this; }, @@ -410,6 +410,12 @@ window.wp = window.wp || {}; }, _validateHandler: function( attachment, attachments, options ) { + // If we're not mirroring this `attachments` collection, + // only retain the `silent` option. + options = attachments === this.mirroring ? options : { + silent: options && options.silent + }; + return this.validate( attachment, options ); }, @@ -423,34 +429,21 @@ window.wp = window.wp || {}; this.unmirror(); this.mirroring = attachments; - this.reset( attachments.models ); - attachments.on( 'add', this._mirrorAdd, this ); - attachments.on( 'remove', this._mirrorRemove, this ); - attachments.on( 'reset', this._mirrorReset, this ); + + // Clear the collection silently. A `reset` event will be fired + // when `observe()` calls `validateAll()`. + this.reset( [], { silent: true } ); + this.observe( attachments ); }, unmirror: function() { if ( ! this.mirroring ) return; - this.mirroring.off( 'add', this._mirrorAdd, this ); - this.mirroring.off( 'remove', this._mirrorRemove, this ); - this.mirroring.off( 'reset', this._mirrorReset, this ); + this.unobserve( this.mirroring ); delete this.mirroring; }, - _mirrorAdd: function( attachment, attachments, options ) { - this.add( attachment, { at: options.index }); - }, - - _mirrorRemove: function( attachment ) { - this.remove( attachment ); - }, - - _mirrorReset: function( attachments ) { - this.reset( attachments.models ); - }, - more: function( options ) { if ( this.mirroring && this.mirroring.more ) return this.mirroring.more( options );