Nested records in templates


#1

Hi,

I have a model with a relation to the nested record as follows:

models/row.js

import DS from 'ember-data';

export default DS.Model.extend({
  description: DS.attr("string"),
  branches: DS.hasMany('row')
})

However, when I use a simple each in my templates, I get all the records rendered: both parents and branches. This is my template:

templates/structure.hbs

{{#each row in controller}}
   <li>{{row.description}}</li>
{{else}}

I wanted to implement a partial to print the nested rows in a recursive fashion. But I don’t how to handle the context switching for the each given it’s quite sophisticated. Or should I stop using the each helper and try to write one on my own?

Thanks!


#2

What’s confusing here is that you seem to be referencing the row model from within row which is unclear and could lead to undefined behavior.

Is the row in the hasMany relationship a different model or the same (recursive) one?

Have you tried redefining it to something like:

branches: DS.hasMany('branch')

#3

I am indeed trying to have a row with many rows, as a recursive definition. I guess I am experimenting the undefined behavior now. If this is not valid, is there any strategy compatible with ember to tackle recursive relationships?

EDIT: I realized there is really no undefined behaviour when using recursive models. The issue I was experimenting was in the route when I loaded the model for the controller.