Ember.js 如何根据表达式的结果默认余烬模型的值?

Ember.js 如何根据表达式的结果默认余烬模型的值?,ember.js,model,expression,default,Ember.js,Model,Expression,Default,下面的代码给出了一个错误说明: 未捕获类型错误:未定义不是函数 有人能帮我正确地构造它,这样我就可以基于这个表达式默认一个值吗 ,defaultPersonType: function defaultPersonType() { console.log(this) var people = this.get("store").all("person").content ,primaryFound = false ,s

下面的代码给出了一个错误说明: 未捕获类型错误:未定义不是函数

有人能帮我正确地构造它,这样我就可以基于这个表达式默认一个值吗

,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()
    }())
嗯,你的

this
匿名函数中存在错误。因此,请这样做:

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

我认为实现此逻辑的最佳位置是路线的模型挂钩。

您不应该访问Ember模型内的存储。那么您将如何实现?