In my application I have an item called block which can contain and be contained by other block items. To keep things simple, lets say these blocks can be infinitely nested.
I want to know if I can create recursive views corresponding to the blocks that are also nested. Each view will be rendered as a DIV
containing its children and residing inside its parent DIV
?
While the views are similar in terms of what they can contain, their actual content that will be obtained from the server can be different. Here is an example data:
App.blocks.set('content',[
Em.Object.create({title:"One", id:"1", is_inside:"0"}),
Em.Object.create({title:"Two", id:"2", is_inside:"1"}),
Em.Object.create({title:"Three", id:"3", is_inside:"0"}),
Em.Object.create({title:"Four", id:"4", is_inside:"3"}),
Em.Object.create({title:"Five", id:"5", is_inside:"4"})
])
In this example, block one would be rendered as root (assume 0 is root which means not inside any other block). Block two would be rendered inside block one and so on.
I noticed a similar question has been asked a while ago, but I am not satisfied with the answer there. I feel there must be an elegant way to achieve this in Ember.
Can you point me to an example of how this can be done in Ember? If not, any advice that could help me make progress and refine my question would also be very appreciated.
Edit: Here is a jsFiddle that I made with some initial data that we could use as starting point. With your help hopefully I can make those DIVs
nested based on their is_inside relation. I have updated the post to use this simpler example.
Take this as a hint, I think the
layout
property ofEmber.View
comes in rescue...It works this wayWill result in the following html
Model your data first, then use
for
orwhile
loops in js to create views, I don't think using handlebar helpers for recursion is a good idea.Edited Section There can be a better way than this...but check this out, and here you are not limiting the nesting to one level, right ?