javascript - Backbone.js: How do I identify the model of the parent view upon an event? -
i have 2 collections instantiated: items , sections.
each section can have multiple items , each item can occur in more 1 section.
when add item section want hash of item , save array attribute of section. array attribute used determine items displayed in each section.
so let's have itemlistmodel 100 items , have section 'items' attribute array [ item3_id_hash, item27_id_hash, item59_id_hash ]. when display sectionview, first map hash ids items collection objects , create itemsview displays objects.
the problem have in identifying items upon creation , upon deletion can ids , add/remove them section.item attribute array.
i can think of hackish solution identify item upon addition setting global variable called parent. want avoid using globals however.
removing matter entirely. when user clicks x delete item in view, can't figure out how can either identify parent section's model, or @ least identify id.
example view:
- section_1
- item_1
- item_2
- item_3
- section_2
- item_4
- item_5
- item_6
if user deletes item_2 view, how can section_1.model.get('id') can section_1 object , remove 'id' item_2 'items' array attribute in section_1
the solution can think of passing 'id' section_1 html view when draw itemsview section_1. hackish , fragile trying avoid it.
fyi: sectionlistview instantiates 1 or more sectionsviews, each of instantiate 1 itemlistview, instantiates 1 or more itemviews
you should go model. if want remove item, remove model list , have different view listen removal events.
so section observing list remove event , act accordingly (either remove element on page or re-render self completely).
models , events (alternatively, routes) should preferred glue code in situation.
i hope understood question.
Comments
Post a Comment