2014-09-19 89 views
1

目前正在开发一个使用Ember-cli的应用程序,并且使用2个单词和关系模型有一些困难。Ember cli - 由多个单词和关系组成的模型(ember-data)

型号居民瞩目

//models/residents-profile.js 
DS.Model.extend({ 
    firstName: DS.attr('string'), 
    lastName: DS.attr('string'), 
    picture: DS.attr('string'), 
    phone: DS.attr('string'), 
    gender: DS.attr('string'), 
    residentsAccount: DS.belongsTo('residentsAccount') 
} 

**型号居民账户**

//models/residents-account.js 
DS.Model.extend({ 
    email: DS.attr('string'), 
    password: DS.attr('string'), 
    profileId: DS.attr('number'), 
    residentsProfile: DS.belongsTo('residentsProfile', this.profileId), 
}); 

对居民路线模型钩:

//routes/residents.js 

    model: function() { 
      return Ember.RSVP.hash({ 
       residentsProfile: this.store.find('residentsProfile'), 
       residentsAccount: this.store.find('residentsAccount') 
      }) 
    } 

由于尽快尝试和fetc h只是居民的个人资料,我得到一个错误“居民。索引不能读取财产'typeKey'”

但是,如果我从居民配置文件中删除关系密钥,并只打电话给居民配置文件数据被正确提取。

我使用的RESTAdapter和一个RESTful API,

这些模型被单独返回和来自服务器的响应如下所示:

GET/residentsProfile { “residentsProfiles” :[{ “ID”:20, “图片报”:空, “手机” 日期null, “名字”: “洛奇”, “姓氏”: “巴尔博亚”, “块标识”:空, “unitId”:null, “createdAt”:“2014-09-17 19:54:28”, “updatedAt”:“2014-09-17 19:54:28”, “residentaccount”:[5 ] }] }

GET/residentsAccount { “residentsAccounts”:[{ “ID”:5, “电子邮件”: “[email protected]”, “管理员”:假, “resident”:true, “profileId”:20, “createdAt”:“2014-09-17 19:54:29”, “updatedAt”:“2014- 09-17 19" 时54分29秒, “residentsProfile”:[20] }] }

EDIT

除了由@ Kingpin2k提出的修改,这是点上,我使用setupcontroller以下列方式:

setupController: function(controller, 
     controller.set('residentsAccount', models.residentsAccount); 
     controller.set('residentsProfile', models.residentsProfile); 
    } 

现在一切正常。

回答

2

三个问题,无论你的关系应该是异步(因为他们并不在同一个响应返回)

residentsAccount: DS.belongsTo('residentsAccount', {async: true}) 

residentsProfile: DS.belongsTo('residentsProfile', {async:true}) 

而且无论是从他们的JSON响应应该是一个单一的ID,而不是一个数组

{ 
    "residentsProfiles":[ 
    { 
    "id":20, 
    "picture":null, 
    "phone":null, 
    "firstName":"Rocky", 
    "lastName":"Balboa", 
    "blockId":null, 
    "unitId":null, 
    "createdAt":"2014-09-17 19:54:28", 
    "updatedAt":"2014-09-17 19:54:28", 
    "residentsAccount": 5 
    } 
    ] 
} 

最后,我不知道你想在这里与this.profileId实现的目标,但它可能不是做什么,你认为它是。该范围内的this可能是窗口,这意味着您传递的可能性不明确。

+0

你是绝对正确的,我只是粘贴了我在编辑器中的任何东西(拼命地)试图调试。所以肯定这是一个错误。我正在等待后端人员修复api来测试它,然后将您的答案标记为正确。 非常感谢您的回复! – bzlies 2014-09-19 15:30:26