这似乎是显而易见的。我不知道我哪里错了。
在 Controller 中,对于计算属性:
totalMonthlyEsales: (->
@findBy('key', 'value1')
).property('@each.answer')
我可以在我的模板中请求对此的属性。
<div>{{totalMonthlyEsales.answer}}</div>
返回“23424”
但是如果我尝试
totalMonthlyEsales: (->
@findBy('key', 'value1').get('answer')
).property('@each.answer')
我收到错误“未捕获类型错误:无法调用未定义的方法“get”” 最终我想做一些类似的事情
totalMonthlyEsales: (->
parseInt @findBy('key', 'value1').get('answer')
).property('@each.answer')
plccDcSalesCash: (->
parseInt @findBy('key', 'value2').get('answer')
).property('@each.answer')
otherTenderTypes: (->
@get('plccDcSalesCash') - @get('totalMonthlyEsales')
).property('totalMonthlyEsales', 'plccDcSalesCash')
最佳答案
我的猜测是,ArrayController 的内容是异步填充的,尽管最终最终会得到该计算属性返回的值(具有与其关联的答案),但当计算属性首先触发时,不是匹配值,因此它返回 undefined,当然,您不能对未定义值调用 .get()
。由于您使用的是 CoffeeScript,因此您可以执行以下操作:
totalMonthlyEsales: (->
@findBy('key', 'value1')?.get('answer')
).property('@each.answer')
?
表示仅当 findBy()
返回的值为真时才尝试方法调用。
关于javascript - Ember Array 调用 get 就返回 findBy 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21243080/