user240993
user240993

Reputation:

Backbone: How do I put a collection inside a view

I see many tutorials which don't follow the supposedly best practice of making a model, a view and collection for that model then a view for the collection. Which would be the parent view?

How do I make a view for a collection? Also, is it possible for it to keep track of when a model is added or deleted for it to update/re-render?

Upvotes: 0

Views: 95

Answers (1)

neiker
neiker

Reputation: 8991

You must do something like this in your collection view:

var view = Backbone.View.extend({});
var myView = new view({'collection' : new collection});

To handle add/remove event, use this in your initialize function:

this.collection.on("add", this.onAdd, this);   
this.collection.on("remove", this.onRemove, this);   

and in your model view:

this.model.on("change", this.onUpdate,this);

See it here: http://www.neiker.com.ar/backbone/

(Sorry, I don't speak english)

EDIT: Just use marionette: https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.collectionview.md

Upvotes: 2

Related Questions