Ember Nested Model - How to get or set values


#1

I have a nested model which is used to construct the tree view.

Here, I have two action handlers which is used to delete the folders based on the passed “id” param.

So when I pass the first level folder “id”, I can remove the elements from the model using filterBy().

But when I pass the second/third level folder “id”, and use filterBy to filter through the model using “id”, it returns an empty value.

Here, how can I can filter through the sub level model using id?

In Template:

<script type="text/x-handlebars" data-template-name="index">
    <button {{action deleteFolder_1 '1'}}>Delete Folder_1</button> 
    <br><br>

    <button {{action deleteFolder_11 '11'}}>Delete Folder_11</button>
    <ul>
        {{#each model}}
            {{partial "tree"}}
        {{/each}}
    </ul>
</script>

<script type="text/x-handlebars" data-template-name="_tree">
    <li>{{foldername}}</li>
    <ul>
    {{#each children}}
        {{partial "tree"}}
    {{/each}}
    </ul>
 </script>

In App.js:

App.IndexRoute = Ember.Route.extend({
    model: function() {
    return [
        {
        id: '1',
        foldername: 'Folder 1',
        children:[{
            id: '11',
            foldername: 'Sub Folder 11',
            children: [{
                id: '111',
                foldername: 'Sub Folder 111',
                children: []
            }]
       }]
   }, 
   { 
   id: '2',
   foldername: 'Folder 2',
   children: []
   },
   {
   id: '3',
   foldername: 'Folder 3',
   children:[]
   }];
   },
   actions: { 
        deleteFolder_1: function (id) {
        var obj = this.controller.content.filterBy('id',id);
        this.controller.content.removeObjects(obj);
   },
  deleteFolder_11: function (id) {
      var obj =      this.controller.content.filterBy('id',id);
      this.controller.content.removeObjects(obj);
      // How can I delete the subfolder here? How can I find the sub id? Is there any way to find id recursively.
}
}
});

JSBIN Demo

PS: My Colleague asked the same question in SO, but still I could not get a response. Hence posting here for a wider audience.