与backbone.js 的多对多关系事件
我与在服务器端使用数据透视表实现的两个backbone.js 模型建立了多对多关系。我正在尝试弄清楚如何在客户端构建它。我当前的结构是:
1)我有一个标签模型和一个呈现复选框和标签标签的TagView,我在复选框上有一个选中事件,目前什么也不做。
- 我有一个 TagsCollection,其中包含一堆标签。
- 我有一个 TagsCollectionView,它绑定 TagsCollection 的添加、重置等,并为添加的标签添加 TagView,呈现它们,并将 html 附加到其当前 html (重置时,html 被重置)。
- 我有一个全局 TagCollection 实例,其中包含所有可能的标签
- 我有一个 Notes 模型,其中包含一个在 init 上称为 selectedtags 的(空)TagCollection。
- 服务器为每个注释返回一组选定的 tagid,我将其添加到其 TagCollection 中。
- 现在是最困难的部分,将它们捆绑在一起..我的NotesView有自己的TagsCollectionView,它绑定到全局TagsCollection(因此它可以列出所有标签)..现在,我如何在其复选框上获取选中事件子 TagViews 来触发添加到此 Notes 模型的选定标签?我是否应该向 init 上的 TagsCollectionView 提供对此 Notes 模型实例的引用,然后将其提供给它创建的所有 TagView,然后其选中的事件从该模型添加/删除项目?这是我能弄清楚如何做到这一点的最好方法,任何其他想法将不胜感激。
I have a many-to-many relationship with two of my backbone.js models implemented using a pivot table on the serverside. I'm trying to figure out how to structure it clientside. My current structure is:
1) I have a Tag model, and a TagView which renders a checkbox and the tag label, I have a checked event on the checkbox which does nothing at the moment.
- I have a TagsCollection, which holds a bunch of Tags.
- I have a TagsCollectionView, which binds add, reset etc of the TagsCollection, and adds TagViews for the added Tags, renders them, and appends the html to its current html (on reset, the html is reset).
- I have a global TagCollection instance which contains all the possible tags
- I have a Notes Model which contains an (empty) TagCollection called selectedtags on init.
- The server returns an array of selected tagids for each Notes, which I add to its TagCollection.
- Now comes the hard part, tying it all together.. my NotesView has its own TagsCollectionView which is bound to the global TagsCollection (so it can list all the Tags).. now, how do I get a checked event on the checkedbox of its sub TagViews to trigger an add to this Notes model's selectedtags? Should I provide a reference to the this Notes model instance to the TagsCollectionView on init which then provides it to all the TagViews it creates, whose checked event then adds/removes items from that model? That's the best way I can figure out how to do this, any other thoughts would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
视图仅用于模型的视觉显示。请更详细地指定对 TagsCollectionView 的需求:
由于缺乏需求细节,我无法提供整个设计。但是,我认为下面的代码应该触发您设计的起点。
View is only for visual display of model. Please specify the need for TagsCollectionView in more details:
I can not provide the whole design due to lack of requirement details. but, I think below code should trigger the starting point of your design.