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.

2
  • If you have a well defined properties model belongsTo is the way to go. Commented Apr 4, 2013 at 11:19
  • So then {{node.properties.age}} would be the right way to access this? Commented Apr 4, 2013 at 11:40

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.