3
votes

I am trying iterate json response which more likely mentioned below and I want achieve this model through reflexive relation.

 {
   folders : [

           { 
             id : 1,
             folders [  { id : 1, folders : [] }  ]
           },
           { 
             id : 2,
             folders : [{ id : 1, folders : [ {id:1 , folders : [] }] }]

           }

    ]
  }

I here is my try children: DS.hasMany('folders', {inverse: 'parent'}), parent: DS.belongsTo('folders', {inverse: 'children'})
But does't work at all . is there any example ?

1

1 Answers

1
votes

I have a similar structure for nested categories modeled like this

In my models/category.js

export default DS.Model.extend({
    name: DS.attr('string'),
    description: DS.attr('string'),
    master: DS.belongsTo('category', {inverse: null})
});

Then in my routes/products.js I define the model hook like this

model: function() {
  return {
    products: this.store.findAll('product'),
    categories: this.store.findAll('category')
  };
}

From controllers/products.js I have access to categories and their master categories like this

var categories = this.get('model').categories;
  for (var i=0; i < categories.get('length'); i++) {
    var master = categories.objectAt(i).get('master').get('id');

It seems that ember takes care of everything in the background somehow.