手动Ember计算属性在辅助属性时不起作用

时间:2014-04-24 04:45:59

标签: ember.js coffeescript

我有以下具有许多计算属性的控制器,其中大多数都使用辅助函数。所有这些都在模板中引用时完美无缺。但是,如果我将它们添加到模板中,则两个手动构造的计算属性将失败,因为它们在第一次运行该函数时未引用它们引用的属性。我从numeral.js得到Uncaught TypeError: Cannot read property 'length' of undefined,因为如果它。我构建它的方式有什么不对?为什么使用帮助程序构建的计算属性可以正常引用相同的基础模型数据?

  AdvisorAccountsController = Ember.ArrayController.extend
    open:          Ember.computed.filterBy "model", "status", "Open"
    pending:       Ember.computed.filterBy "model", "status", "Pending"
    openValues:    Ember.computed.mapBy "open", "value"
    pendingValues: Ember.computed.mapBy "pending", "value"
    openCount:     Ember.computed.alias "open.length"
    pendingCount:  Ember.computed.alias "pending.length"
    openTotal:     Ember.computed.sum "openValues"
    pendingTotal:  Ember.computed.sum "pendingValues"
    showPending:   Ember.computed.gt "pendingCount", 0
    openTotalUSD: (-> numeral(@get "openTotal").format "$0,0.00").property "openTotal"
    pendingTotalUSD: (-> numeral(@get "pendingTotal").format "$0,0.00").property "pendingTotal"

我认为发生的事情是该模型在被解雇时仍在解决。这对于s​​um属性来说不是问题,因为结果值不会被其他东西所依赖,并且它们可以在他们自己的时间内解决。

1 个答案:

答案 0 :(得分:1)

openCount: function(){
    return open && open.length;
    }.property('open.length')

因此,只有length被定义且“真实”时,您才会尝试访问open open

相关问题