Fixed onAfterSplice in es.DocumentViewBranchNode to update the DOM correctly

Fixed issue where events were being listened to from this instead of this.model
Added rendering calls after structural changes
This commit is contained in:
Trevor Parscal 2011-11-14 19:50:04 +00:00
parent 521a0644ff
commit 872d2bdb9d

View file

@ -25,7 +25,7 @@ es.DocumentViewBranchNode = function( model, $element, horizontal ) {
} }
// Observe and mimic changes on model // Observe and mimic changes on model
this.addListenerMethods( this, { this.model.addListenerMethods( this, {
'afterPush': 'onAfterPush', 'afterPush': 'onAfterPush',
'afterUnshift': 'onAfterUnshift', 'afterUnshift': 'onAfterUnshift',
'afterPop': 'onAfterPop', 'afterPop': 'onAfterPop',
@ -55,6 +55,7 @@ es.DocumentViewBranchNode.prototype.onAfterPush = function( childModel ) {
} }
this.emit( 'afterPush', childView ); this.emit( 'afterPush', childView );
this.emit( 'update' ); this.emit( 'update' );
this.renderContent();
}; };
es.DocumentViewBranchNode.prototype.onAfterUnshift = function( childModel ) { es.DocumentViewBranchNode.prototype.onAfterUnshift = function( childModel ) {
@ -68,6 +69,7 @@ es.DocumentViewBranchNode.prototype.onAfterUnshift = function( childModel ) {
this.$.prepend( childView.$ ); this.$.prepend( childView.$ );
this.emit( 'afterUnshift', childView ); this.emit( 'afterUnshift', childView );
this.emit( 'update' ); this.emit( 'update' );
this.renderContent();
}; };
es.DocumentViewBranchNode.prototype.onAfterPop = function() { es.DocumentViewBranchNode.prototype.onAfterPop = function() {
@ -80,6 +82,7 @@ es.DocumentViewBranchNode.prototype.onAfterPop = function() {
childView.$.detach(); childView.$.detach();
this.emit( 'afterPop' ); this.emit( 'afterPop' );
this.emit( 'update' ); this.emit( 'update' );
this.renderContent();
}; };
es.DocumentViewBranchNode.prototype.onAfterShift = function() { es.DocumentViewBranchNode.prototype.onAfterShift = function() {
@ -92,26 +95,44 @@ es.DocumentViewBranchNode.prototype.onAfterShift = function() {
childView.$.detach(); childView.$.detach();
this.emit( 'afterShift' ); this.emit( 'afterShift' );
this.emit( 'update' ); this.emit( 'update' );
this.renderContent();
}; };
es.DocumentViewBranchNode.prototype.onAfterSplice = function( index, howmany ) { es.DocumentViewBranchNode.prototype.onAfterSplice = function( index, howmany ) {
var args = Array.prototype.slice( arguments, 0 ); var i,
this.emit.apply( ['beforeSplice'].concat( args ) ); length,
// Update children args = Array.prototype.slice.call( arguments, 0 );
this.splice.apply( this, args ); // Convert models to views and attach them to this node
if ( args.length >= 3 ) {
for ( i = 2, length = args.length; i < length; i++ ) {
args[i] = args[i].createView();
}
}
this.emit.apply( this, ['beforeSplice'].concat( args ) );
var removals = this.children.splice.apply( this.children, args );
for ( i = 0, length = removals.length; i < length; i++ ) {
removals[i].detach();
removals[i].removeListener( 'update', this.emitUpdate );
// Update DOM // Update DOM
this.$.children() removals[i].$.detach();
// Removals }
.slice( index, index + howmany ) if ( args.length >= 3 ) {
.detach() var $target;
.end() if ( index ) {
// Insertions $target = this.$.children().eq( index );
.get( index ) }
.after( $.map( args.slice( 2 ), function( childView ) { for ( i = args.length - 1; i >= 2; i-- ) {
return childView.$; args[i].attach( this );
} ) ); args[i].on( 'update', this.emitUpdate );
this.emit.apply( ['afterSplice'].concat( args ) ); if ( index ) {
this.emit( 'update' ); $target.after( args[i].$ );
} else {
this.$.prepend( args[i].$ );
}
}
}
this.emit.apply( this, ['afterSplice'].concat( args ) );
this.renderContent();
}; };
es.DocumentViewBranchNode.prototype.onAfterSort = function() { es.DocumentViewBranchNode.prototype.onAfterSort = function() {
@ -131,6 +152,7 @@ es.DocumentViewBranchNode.prototype.onAfterSort = function() {
} }
this.emit( 'afterSort' ); this.emit( 'afterSort' );
this.emit( 'update' ); this.emit( 'update' );
this.renderContent();
}; };
es.DocumentViewBranchNode.prototype.onAfterReverse = function() { es.DocumentViewBranchNode.prototype.onAfterReverse = function() {
@ -143,6 +165,7 @@ es.DocumentViewBranchNode.prototype.onAfterReverse = function() {
} ); } );
this.emit( 'afterReverse' ); this.emit( 'afterReverse' );
this.emit( 'update' ); this.emit( 'update' );
this.renderContent();
}; };
/** /**