Backbone.js:视图或集合可以有属性吗?
假设我有一个男孩和女孩名字的列表。该姓名集合还具有“性别”属性。
我想在集合或视图上放置“SelectedGender”属性,以便仅当所选性别发生更改时才可以触发自定义事件。
可以扩展视图以具有触发更改事件的属性吗?
可以扩展集合以具有触发更改事件的属性吗?
或者我是否被迫创建另一个包含集合的模型?我实际上没有代码,我现在只是在考虑设计。
Lets say I have a list of boys and girls names. This collection of names also has a 'Gender' property.
I want to put a 'SelectedGender' attribute on either the Collection or the View so that I can fire a custom event only when the Selected gender changes.
Can a View be extended to have a attribute that fires a change event?
Can a Collection be extended to have a attribute that fires a change event?
Or am I forced to create another Model that contains a collection? I don't actually have code I'm just thinking design at this point.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答您的问题:是否可以扩展视图或集合以具有触发更改事件的属性,答案是否定的。
不,您不能执行
view.set({attr: value})
或collection.set({attr: value})
但是,您可以拥有视图或通过扩展 Backbone.Events 来收集火灾事件。
然而,可能有更好的设计方法。
如果当用户从组合框中进行选择时所选性别发生变化,那么您可以订阅视图中的组合框更改事件,然后让您的视图进行当时需要完成的任何更改。
To answer your question: Can a view or collection be extended to have an attribute that fires a change event, the answer is no and yes.
No, you can't do
view.set({attr: value})
orcollection.set({attr: value})
You can, however have a view or collection fire events by extending Backbone.Events.
However, there might be a better way to design this.
If the selected gender changes when a user makes a selection from a combo box, then you can subscribe to the combo box change event in your view, and then have your view make any changes that need to be done at that time.
我认为您不需要为您的集合创建包装模型。只需创建一个存储姓名和性别的模型即可。然后,就像@Paul提到的那样,您可以订阅集合或视图中的更改事件。
编辑:
与其根据名字的性别拥有 2 个单独的集合,为什么不拥有一个仅返回男孩名字的方法和一个仅返回女孩名字的方法的名字集合呢?
IE:
I don't think you need to create wrapper models for your collections. Just create a model that stores the name and the gender. Then, like @Paul mentioned, you can subscribe to the change event in the collection or in the view.
EDIT:
Rather than having 2 seperate collections depending on the gender of the name, why not have one collection of names with a method that returns just the boy names and a method that returns just the girl names?
ie: