2017-07-19 33 views
1

我有一个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}} 

此外,在控制台灰烬存在的电话号码的数据。我错过了什么?

+0

什么是你的余烬数据的版本? – kumkanillam

+0

'“ember-data”:“〜2.14.3”' – Heisenmali

回答

0

model.PhoneNumbers =>model.phoneNumbers

如果这不只是这个错字,请尝试从JSON API适配器/串行扩展模型串行器/适配器(或做你的应用程序序列化器/适配器上,如果所有的机型使用):

import DS from 'ember-data'; export default DS.JSONAPIAdapter.extend({});

和:

import JSONAPISerializer from 'ember-data/serializers/json-api'; export default JSONAPISerializer.extend({});

+0

感谢您的建议,对于错字感到抱歉。它没有帮助。我不知道这是否会有所帮助,但是我在单个呈现器中在<前端@ model:contact :: ember348:3>上修改了'model.phoneNumbers'两次。'和'Can not read property'getAttribute '在控制台中出现未定义的错误。 – Heisenmali

+0

我现在在'app/adapters'中有一个application.js,其中包含您的第一个建议中的代码。可悲的是没有任何改善。 – Heisenmali