Media: Make priority list views leverage the subview management class. see #21390.

git-svn-id: http://core.svn.wordpress.org/trunk@22802 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Daryl Koopersmith 2012-11-22 00:41:39 +00:00
parent 3be0694d83
commit de54f9318f
1 changed files with 37 additions and 33 deletions

View File

@ -680,10 +680,12 @@
if ( existing ) { if ( existing ) {
if ( options.add ) { if ( options.add ) {
if ( _.isUndefined( options.at ) ) if ( _.isUndefined( options.at ) ) {
next = existing.concat( views ); next = existing.concat( views );
else } else {
next = existing.splice.apply( existing, [ options.at, 0 ].concat( views ) ); next = existing;
next.splice.apply( next, [ options.at, 0 ].concat( views ) );
}
} else { } else {
_.each( next, function( view ) { _.each( next, function( view ) {
view.__detach = true; view.__detach = true;
@ -736,6 +738,12 @@
// //
// For more information on the `options` object, see `Views.set()`. // For more information on the `options` object, see `Views.set()`.
add: function( selector, views, options ) { add: function( selector, views, options ) {
if ( ! _.isString( selector ) ) {
options = views;
views = selector;
selector = '';
}
return this.set( selector, views, _.extend({ add: true }, options ) ); return this.set( selector, views, _.extend({ add: true }, options ) );
}, },
@ -2184,42 +2192,22 @@
this.set( _.extend( {}, this._views, this.options.views ), { silent: true }); this.set( _.extend( {}, this._views, this.options.views ), { silent: true });
delete this.options.views; delete this.options.views;
if ( ! this.options.silent ) // if ( ! this.options.silent )
this.render(); this.render();
}, },
destroy: function() { destroy: this.dispose,
this.remove();
_.each( this._views, function( view ) {
if ( view.destroy )
view.destroy();
});
},
render: function() {
var els = _( this._views ).chain().sortBy( function( view ) {
return view.options.priority || 10;
}).pluck('el').value();
// Make sure to detach the elements we want to reuse.
// Otherwise, `jQuery.html()` will unbind their events.
$( els ).detach();
this.$el.html( els );
return this;
},
set: function( id, view, options ) { set: function( id, view, options ) {
var priority, views, index;
options = options || {}; options = options || {};
// Accept an object with an `id` : `view` mapping. // Accept an object with an `id` : `view` mapping.
if ( _.isObject( id ) ) { if ( _.isObject( id ) ) {
_.each( id, function( view, id ) { _.each( id, function( view, id ) {
this.set( id, view, { silent: true }); this.set( id, view );
}, this ); }, this );
if ( ! options.silent )
this.render();
return this; return this;
} }
@ -2228,9 +2216,23 @@
view.controller = view.controller || this.controller; view.controller = view.controller || this.controller;
this.unset( id );
priority = view.options.priority || 10;
views = this.views.get() || [];
_.find( views, function( existing, i ) {
if ( existing.options.priority > priority ) {
index = i;
return true;
}
});
this._views[ id ] = view; this._views[ id ] = view;
if ( ! options.silent ) this.views.add( view, {
this.render(); at: _.isNumber( index ) ? index : views.length || 0
});
return this; return this;
}, },
@ -2238,10 +2240,12 @@
return this._views[ id ]; return this._views[ id ];
}, },
unset: function( id, options ) { unset: function( id ) {
var view = this.get( id );
if ( view )
view.dispose();
delete this._views[ id ]; delete this._views[ id ];
if ( ! options || ! options.silent )
this.render();
return this; return this;
}, },