1
的代碼給出了一個錯誤說: 遺漏的類型錯誤:未定義是不是一個函數如何根據表達式的結果來默認Ember模型的值?下面
有人可以幫助我正確地構建,這樣我可以默認基於此表達式的值?
,defaultPersonType: function defaultPersonType() {
console.log(this)
var people = this.get("store").all("person").content
,primaryFound = false
,spouseFound = false
,dependantFound = false
,defaultType = "CHILD"
for (var i = 0; i < people.length; i++) {
switch(people.get("personType")) {
case "PRIMARY":
primaryFound = true
break
case "SPOUSE":
spouseFound = true
break
case "UNMARRIED_PARTNER":
spouseFound = true
break
default:
dependantFound = true
break
}
if (!primaryFound) {
defaultType = "PRIMARY"
}
if (!!dependantFound) {
defaultType = "CHILD"
}
if (!spouseFound) {
defaultType = "SPOUSE"
}
}
return DS.attr('string', {defaultValue: function() {return defaultType}})
}
,personType: (function() {
return this.defaultPersonType()
}())
您不應該在Ember模型中訪問商店。 – quaertym
那麼你會怎麼做呢? – Jaime