2014-09-19 100 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