2014-04-11 24 views
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() 
    }()) 
+0

您不應該在Ember模型中訪問商店。 – quaertym

+0

那麼你會怎麼做呢? – Jaime

回答

0

那麼,你的

this 

是錯誤的匿名函數。所以,這樣做:

function() { return this.defaultPersonType(); }.bind(this)()) 
0

我認爲最好的地方來實現這個邏輯是在路線的模型鉤子。