I am using ember 2.8.0 and ember-data 2.8.0. I have the following models defined:
//app/models/store.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
floor: DS.belongsTo('floor'),
number: DS.attr('string'),
phone: DS.attr('string'),
email: DS.attr('string'),
photo: DS.attr(),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date')
});
//app/models/floor.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
stores: DS.hasMany('store'),
createdAt: DS.attr('date'),
updatedAt: DS.attr('date')
});
My router.js has the following routes:
//app/router.js
this.route('stores', function() {
this.route('new');
this.route('edit');
});
My templates/stores/index.hbs has the following {{#each}} statement:
<tbody>
{{#each model as |store|}}
<tr>
<td>{{store.name}}</td>
<td>{{store.number}}</td>
<td>{{store.floor.name}}</td>
<td>{{store.phone}}</td>
<td>{{store.email}}</td>
<td>{{moment-format store.createdAt}}</td>
<td>{{moment-format store.updatedAt}}</td>
</tr>
{{/each}}
</tbody>
I would have expected that the line {{store.floor.name}} would have made a request to /floors/{id} for each and every row in the table.
What am I missing? What's the proper way to handle that in Ember?
Should my api return something different than the below?
[{"id":1,"floorId":1,"name":"McDonalds","number":"10-A","phone":"(11) 2020-3455","email":"[email protected]","photo":null,"createdAt":"2016-09-15T13:45:32.000Z","updatedAt":"2016-09-15T13:45:32.000Z"}]
Should I use other model hooks to load the associated model manually?
storemodel need to match what the server gives you, unless you have defined some special processing for them in adapters/serializers/somewhere (I forget where exactly this happens, now), which seems unlikely. So, returnfloorinstead offloorIdand things may just begin to work. - ThernysfloortofloorIddoesn't. Would that be some ember-data convention thingy? Also, care to answer the question? - Andre Gallo