开发者

Handling multiple instance of a collection?

Do I need to bind to every collection that is instantiated of the same type or do I bind to a common change event that passes in a reference to itself?

example: What would an interface with 5 different todo lists look like? Would they each need a unique id? I'm guessing they would be placed into another collection of todo lists? Any code e开发者_开发问答xamples would be great. Thanks.

Edit: Sorry if I'm still not clear.

TodoList is a collection of Todo models.

My app needs to display any given number of TodoList's on it. What is the best way to organize different instances of these?


generally speaking, you don't need to do anything special to handle multiple instances of a collection.


MyCollection = Backbone.Collection.extend({ ... });

MyView = Backbone.View.extend({ ... });

c1 = new MyCollection();
c2 = new MyCollection();

v1 = new MyView({collection: c1});
v2 = new MyView({collection: c2});

each view will only reference it's collection, and there is no need to worry about data being mis-matched between the two collections. when you add a model to one, it won't show up in the other. when you manipulate the view for one, it won't manipulate the view for the other (assuming you've written your view correctly).

you'll need a to coordinate how each view for each collection is displayed on the screen. this would work like any collection / item view setup, where you have a collection view that instantiates each child view, renders the child view, and then displays the child view's el within it's own el.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜