This is an example of using a Collection view with Backbone.
Created
May 30, 2012 12:47
-
-
Save bergie/2836052 to your computer and use it in GitHub Desktop.
Backbone.js Collection View example
Thanks for those snippets, the switch from Ember to Backbone is not so straight-forward sometimes !
@bergie - very nice & clean dude. One note though, rendering the whole collection when a person is added/removed may be overkill.
@yattias could you please tell whether there are opportunities to avoid the problem you've mentioned (with Backbone of course)?
@vitalyisaev2 you can create your own listener for collection's 'add' event, which will insert new PeopleItem view in correct place:
onCollectionItemAdd: function ( model, collection ) {
var list = this.$( 'list selector' ),
listItems = list.children(),
newItem = self._renderListItem( model ),
newItemPosition = collection.models.indexOf( model );
if ( newItemPosition > listItems.length - 1 ) {
newItem.$el.appendTo( list );
} else {
newItem.$el.insertBefore( listItems.eq( newItemPosition ) );
}
}
Then you can add listener for model's 'remove' event in PeopleItem view:
onModelRemove: function() {
this.remove();
}
Just what I needed. Thanks!
Doesn't this solution have DOM Reflow? Here you are attaching child view html with parent view html, which causes DOM Reflow?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clean and concise solution! Thanks, man. Table rendering drives me nuts.