I would like to have model binding relationships loaded automatically when referenced in a template. For example, if I have models like this:
App.User = DS.Model.extend
name: DS.attr 'name'
App.Contact = DS.Model.extend
addedBy: DS.belongsTo 'App.User'
and a view like this:
<div>{{contact.addedBy.name}}</div>
it would be really nice if ember-data caught on that it needs to load the User with a primary key in “addedBy”. Currently I have to load the User manually with App.User.find(contact.get(‘addedBy’)) and then the template binding updates to display the user’s name.
This is a very simple example but in practice I sometimes find myself traversing relationships pretty far. Is there an easy way to automate this?
Thanks folks!
Turns out that ember-data does exactly what I want by default, and the problem was a bug in my code.
Make sure that the backend for your adapter’s findMany() method returns the records in the same order as the argument array of IDs otherwise your DS.hasMany relationships will act very very weird!