ember.js - ember-data render belongsTo attribute -
ember-cli 0.2.3; ember-data 1.0.0-beta.15
i'm struggling figure out how display attributes belongsto object inside template. i've found few other similar questions here they're either out of date or don't provide solution.
user model: export default ds.model.extend({ ... agency : ds.belongsto("agency"), });
agency model: var agency = ds.model.extend({ ... users: ds.hasmany("user"), });
user serializer: export default applicationserializer.extend(ds.embeddedrecordsmixin, { attrs: { agencies: { embedded: "always" } } });
applicationserializer: export default ds.restserializer.extend({});
the json (for index route) looks so: { "users": [ { "id" : "1", "name" : "...", "agency_id": "6", "agencies" : [ { "id" : "6", "name": "...", ... } ] }, ... ] }
although ember isn't complaining, i'm not sure data coming in correctly because documentation embeddedrecordsmixin shows belongsto example as: { "post": { "id": "1" "title": "rails omakase", "author": { "id": "2" "name": "dhh" } } }
in case, can see agency data included, although seems has array, though there ever single 1 per user. haven't been able find complete explanation of precisely format expected.
however, main thing is: how heck display agency name in template?
unsurprisingly, doesn't work: {{#each user in model}} <tr> <td>{{user.get('agencies').firstobject().get("name")}}</td> <td>{{user.name}}</td> <td>{{user.email}}</td> </tr> {{/each}}
update: i'd modified data this: { "users": [ { "id" : "1", "name" : "...", "agency_id": "6", "agency" : [ { "id" : "6", "name": "...", ... } ] }, ... ] }
... , updated model have agency: ds.belongsto("agency") , serializer attrs: {agency: {embedded: "always"}} threw error. solution remove agency_id data.
Comments
Post a Comment