Backbone.js:如何在事件发生时识别父视图的模型?
我实例化了两个集合:项目和部分。
每个部分可以有多个项目,并且每个项目可以出现在多个部分中。
当我将一个项目添加到一个部分时,我想获取该项目的哈希值并将其保存到该部分的数组属性中。该数组属性用于确定每个部分中显示哪些项目。
假设我有一个包含 100 个项目的 ItemListModel,并且有一个带有“items”属性的部分,其中包含数组 [ item3_id_hash、item27_id_hash、item59_id_hash ]。当我显示SectionView 时,我首先将这些哈希ID 映射到Items 集合以获取这些对象,然后创建一个仅显示这些对象的ItemsView。
我遇到的问题是在创建和删除时识别这些项目,以便我可以获得它们的 id 并从section.item 属性数组中添加/删除它们。
我可以想到一个黑客解决方案,通过设置一个名为“parent”的全局变量来在添加时识别该项目。但是我想避免使用全局变量。
删除完全是另一回事。当用户单击 X 删除视图中的项目时,我不知道如何识别父部分的模型,或者至少识别其 ID。
示例视图:
- Section_1
- 项目_1
- 项目_2
- 项目_3
- 部分_2
- 项目_4
- 项目_5
- 项目_6
如果用户从视图中删除 Item_2,我怎样才能获得Section_1.model.get('id'),以便我可以获得section_1对象,然后从'items中删除Item_2的'id' 'Section_1 中的数组属性
我能想到的唯一解决方案是,当我为Section_1 绘制ItemsView 时,将Section_1 的“id”传递到HTML 视图。这是黑客和脆弱的,所以我试图避免它。
仅供参考:SectionListView 实例化一个或多个SectionsView,每个SectionsView 实例化一个ItemListView,ItemListView 实例化一个或多个ItemView
I have two collections instantiated: items and sections.
Each section can have multiple items and each item can occur in more than one section.
When I add an item to a section I want to get the hash of that item and save it to an array attribute of the section. This array attribute is used to determine which items are displayed in each section.
So let's say that I have a ItemListModel with 100 items and I have a section with an 'items' attribute with the array [ item3_id_hash, item27_id_hash, item59_id_hash ]. When I display the SectionView, I first map those hash ids to the Items collection to get those objects and then I create an ItemsView that displays only those objects.
The problem I have is in identifying those Items upon creation and upon deletion so that I can get their ids and add/remove them from the section.item attribute array.
I can think of a hackish solution to identify the item upon addition by setting a global variable called parent. I want to avoid using globals however.
Removing is another matter entirely. When the user clicks the X to delete an item in the view, I can't figure out how I can either identify the parent Section's model, or at the very least identify its ID.
Example view:
- Section_1
- Item_1
- Item_2
- Item_3
- Section_2
- Item_4
- Item_5
- Item_6
If the user deletes Item_2 from the view, how can I get Section_1.model.get('id') so I can get the section_1 object and then remove the 'id' for Item_2 from the 'items' array attribute in Section_1
The only solution I can think of is passing the 'id' for Section_1 to the HTML view when I draw ItemsView for Section_1. This is hackish and fragile so I was trying to avoid it.
FYI: SectionListView instantiates one or more SectionsViews, each of which instantiate one ItemListView, which instantiates one or more ItemViews
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该总是回到模型。如果要删除某个项目,请将其从模型列表中删除,并让不同的视图侦听删除事件。
因此,您的部分正在观察 List 删除事件并采取相应的行动(删除页面上的元素或完全重新渲染它自身)。
在大多数情况下,模型和事件(或者路线)应该优先于粘合代码。
我希望我理解你的问题。
You should always go back to the model. If you want to remove an item, then remove it from the model list and have the different view listen for the removal events.
So your section is observing the List remove event and act accordingly (either remove the element on the page or re-render it self completely).
Models and events (alternatively, routes) should be preferred to glue code in most situation.
I hope I understood your question.