2

I have the following json data:

{
    "type": "type1",
    "name": "Name1",
    "properties": {
        "age": 23,
        "address": "Sample"
    }
}

I am modelling this with Ember Data, as follows:

App.Node = DS.Model.extend({
    type: DS.attr('string'),
    name: DS.attr('string'),
    properties: DS.belongsTo('App.NodeProperties')
});

App.NodeProperties = DS.Model.extend({
    age: DS.attr('number'),
    address: DS.attr('string')
});

Is there a better way to model the nested properties than using a DS.belongsTo? How would I access the age in my templates. I am currently doing

{{node.properties.age}}

But I am not sure this is working.

blueFast
  • 41,341
  • 63
  • 198
  • 344

1 Answers1

4

Is there a better way to model the nested properties than using a DS.belongsTo?

DS.belongsTo is a good choice given your use case.

How would I access the age in my templates?

{{node.properties.age}} is right, assuming that {{node}} is a valid reference

But I am not sure this is working.

There are a few more steps you'll need to take to get this working. First, add a mapping for App.Node to the rest adapter specifying that properties will be embedded:

DS.RESTAdapter.map('App.Node', {
  properties: { embedded: 'always' }
};

Then update NodeProperties to include the relationship:

App.NodeProperties = DS.Model.extend({
  age: DS.attr('number'),
  address: DS.attr('string'),
  node: DS.belongsTo('App.Node')
});

For more info, check out these answers:

Community
  • 1
  • 1
Mike Grassotti
  • 19,040
  • 3
  • 59
  • 57