我有一个rails后端,按照JSON API标准(jsonapi-resources gem)提供数据。我有两种模式:联系人和电话号码。在模板上访问Ember数据有很多关系
class Contact < ApplicationRecord
has_many :phone_numbers
end
class PhoneNumber < ApplicationRecord
belongs_to :contact
end
这是API端点的JSON响应。
{
"data": [
{
"id": "6",
"type": "phone-numbers",
"links": {
"self": "http://localhost:3000/phone-numbers/6"
},
"attributes": {
"name": "byeworld",
"phone-number": "9212098"
},
"relationships": {
"contact": {
"links": {
"self": "http://localhost:3000/phone-numbers/6/relationships/contact",
"related": "http://localhost:3000/phone-numbers/6/contact"
}
}
}
}
]
}
这些都是我的灰烬型号:
的接触:
import DS from 'ember-data';
export default DS.Model.extend({
nameFirst: DS.attr('string'),
nameLast: DS.attr('string'),
email: DS.attr('string'),
twitter: DS.attr('string'),
phoneNumbers: DS.hasMany('phone-number', {async: true, inverse: 'contact'})
});
的电话号码:
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
phoneNumber: DS.attr('string'),
contact: DS.belongsTo('contact', {async: true, inverse: 'phoneNumbers'})
});
这是我的路由处理:
import Ember from 'ember';
export default Ember.Route.extend({
model(params) {
return this.store.findRecord('contact', params.contact_id, {include: "phone-numbers"});
},
(...)
});
我不能做访问电话号码,在模板上的联系人:
{{#each model.phoneNumbers as |phone|}}
{{phone.name}}
{{/each}}
此外,在控制台灰烬存在的电话号码的数据。我错过了什么?
什么是你的余烬数据的版本? – kumkanillam
'“ember-data”:“〜2.14.3”' – Heisenmali