简体   繁体   English

Backbone.js:如何确定事件后父视图的模型?

[英]Backbone.js: How do I identify the model of the parent view upon an event?

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 ]. 因此,假设我有一个包含100个项目的ItemListModel,并且我的一个节带有数组[item3_id_hash,item27_id_hash,item59_id_hash]的'items'属性。 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. 当显示SectionView时,我首先将这些哈希ID映射到Items集合以获取那些对象,然后创建一个只显示那些对象的ItemsView。

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. 我的问题是在创建和删除这些项时确定这些项,以便我可以获取它们的ID并从section.item属性数组中添加/删除它们。

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. 当用户单击X删除视图中的项目时,我不知道如何识别父节的模型,或者至少识别其ID。

Example view: 示例视图:

  • Section_1 Section_1
    • Item_1 Item_1
    • Item_2 Item_2
    • Item_3 Item_3
  • Section_2 Section_2
    • Item_4 Item_4
    • Item_5 Item_5
    • Item_6 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 如果用户从视图中删除Item_2,如何获取Section_1.model.get('id'),这样我就可以获取section_1对象,然后从Section_1中的'items'数组属性中删除Item_2的'id'

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. 我能想到的唯一解决方案是,当我为Section_1绘制ItemsView时,将Section_1的'id'传递给HTML视图。 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 仅供参考:SectionListView实例化一个或多个SectionsViews,每个实例化一个ItemListView,实例化一个或多个ItemViews

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). 因此,您的部分将观察List remove事件并采取相应的措施(删除页面上的元素或完全自行重新呈现)。

Models and events (alternatively, routes) should be preferred to glue code in most situation. 在大多数情况下,应首选模型和事件(或者路由)来代替代码。

I hope I understood your question. 希望我能理解您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM