【问题标题】:How can I default a value of an Ember Model based on the result of an expression?如何根据表达式的结果默认 Ember 模型的值?
【发布时间】:2014-04-11 22:36:44
【问题描述】:

下面的代码给出了一个错误提示: Uncaught TypeError: undefined is not a function

有人可以帮我正确地构造它,以便我可以根据这个表达式默认一个值吗?

,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 模型中访问 store。
  • 那你会怎么做呢?

标签: ember.js model expression default


【解决方案1】:

嗯,你的

this

在匿名函数中是错误的。所以,这样做:

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

【讨论】:

    【解决方案2】:

    我认为实现这个逻辑的最佳位置是在路由的模型钩子中。

    【讨论】:

      猜你喜欢
      • 2011-08-24
      • 2011-07-15
      • 2018-11-27
      • 2018-05-23
      • 2021-06-19
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多