Backbone 模型或集合应如何处理视图?
我有一组模型,每个模型都附有一个视图。该系列在全球范围内也享有盛誉。模型视图最好负责删除相应的模型,还是集合(或集合视图)应该这样做?
I have a collection of models, each of which is attached a view. The collection also globally has a view. Is it best for the model views to take care of deleting the corresponding model, or should the collection (or collection view) do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
集合具有
.add
和.remove
方法,当您使用其中之一时,它们会触发add
或remove
> 集合上的事件。这就是您无需使用视图即可进行绑定的方式。因此,将集合附加到构造函数内部的视图。这只是通过
this.collection
使集合可用。这就是使用视图绑定的方式。
当视图初始化时,它会绑定集合的 add 事件来运行 this.add ,这是视图的一个函数。或者,您可以使用 delegateEvents API 来处理将 DOM 元素
事件选择器
映射到在视图中运行该函数的函数
。该函数可以调用 this.collection.add,这将产生多米诺骨牌效应。视图与集合或模型交互的方式是通过绑定到事件,您可以定义这些事件并在视图内处理它们。有几个特殊选项,如果通过,将可用于视图:
model、collection、el、id、className 和 tagName。
Collections have a
.add
and.remove
method, when you use one of them, they fire anadd
orremove
event on the collection. This is how you can bind without using a view at all.So attach the collection to the view, inside of the constructor. This simply makes the collection available via
this.collection
This is how you can bind using the view.
When the view initializes, it binds the collection's add event to run
this.add
which is a function of the view. Optionally, you can use the delegateEvents API to handle mapping DOM elementsevent selector
tofunction
that runs the function in the view. That function can callthis.collection.add
, of which will create the domino effect.The way the view interacts with the collection, or model, is by binding to events, you can define those events and handle them inside the view. There are several special options that, if passed, will be available to the view:
model, collection, el, id, className, and tagName.