2014-09-20 73 views
0

我有兩個型號:灰燼數據嵌入屬於關聯關係,則返回null

Question = DS.Model.extend 

    answers: DS.hasMany("answer") 

Answer = DS.Model.extend 

    question: DS.belongsTo("question") 

JSON的服務嵌入到問題中的答案:

"questions": [{ 
    "id":"1.04" 
    "text":"What is the position or title of the 1% who is being protested against?", 
    "answers":[ 
    { 
     "text":"City mayor", 
     "id":"1.04.02" 
    }, 
    { 
     "text":"City council member", 
     "id":"1.04.03" 
    }, 
    { 
     "text":"CEO of some company", 
     "id":"1.04.01" 
    } 
    ] 
}] 

當我打電話question.get('answers'),灰燼返回的預期陣列答案。但是,如果我撥打answer.get('question'),我會收到空​​。任何想法爲什麼發生這種情況?

回答

1

我想你需要告訴Ember你的關係將被嵌入,這是這樣完成的。

Question = DS.Model.extend 

    answers: DS.hasMany("answer", embedded: 'always') 

Answer = DS.Model.extend 

    question: DS.belongsTo("question", embedded: 'always') 

這樣說,你可能想重新考慮你的json結構,嵌入式記錄會導致很多dublicates。

Ember期望以下開箱即用。

{ 
    "questions": [ 
    { 
     "id": "1.04" 
     "text": "What is the position or title of the 1% who is being protested against?" 
     "answer_ids": ["1.04.02", "1.04.03", "1.04.01"] 
    } 
    ], 
    "answers": [ 
    { 
     "text": "City mayor", 
     "id": "1.04.02", 
     "question_id": "1.04" 
    }, 
    { 
     "text": "City council member", 
     "id": "1.04.03", 
     "question_id": "1.04" 
    }, 
    { 
     "text": "CEO of some company", 
     "id": "1.04.01", 
     "question_id": "1.04" 
    } 
    ] 
} 
+0

從2013年年中起,ED的答案不正確。我添加了正確答案 – IgorT 2014-09-22 21:31:13

0

假設你question.get('answers')的作品,你只需要升級到灰燼數據beta.10,這增加了對雙方合作關係的支持。