我有一个物体,像:
// app/services/my-service.js
import Ember from 'ember';
export default Ember.Service.extend({
counters: Ember.Object.create()
})
myService.counters
是一个散列,类似于:
{
clocks: 3,
diamons: 2
}
我想向这个对象添加一个计算的属性,这样返回myService.counters.clocks
加myService.counters.diamons
的和
// app/services/my-service.js
...
count: Ember.computed('counters.@each', function(){
return _.reduce(this.get('counters'), function(memo, num){ return memo + num; }, 0);
})
...
但是观察者的配置不被接受,我有一个错误:
Uncaught Error: Assertion Failed: Depending on arrays using a dependent key ending with `@each` is no longer supported. Please refactor from `Ember.computed('counters.@each', function() {});` to `Ember.computed('counters.[]', function() {})`.
但如果我提议的改变是:
// app/services/my-service.js
...
count: Ember.computed('counters.[]', function(){
return _.reduce(this.get('counters'), function(memo, num){ return memo + num; }, 0);
})
...
计数属性未更新。
我唯一能做到的就是这样:
// app/services/my-service.js
...
count: Ember.computed('counters.clocks', 'counters.diamons', function(){
return _.reduce(this.get('counters'), function(memo, num){ return memo + num; }, 0);
})
...
在这种情况下,如何使用任何类型的通配符?
发布于 2016-03-07 07:13:39
@each
和[]
用于观察数组元素和数组。
您不能使用通配符,因为它将是一个严重的性能接收器。有一个多属性的缩写:
count: Ember.computed('counters.{clocks,diamons}', function() {
return this.get('counters').reduce((memo, num) => memo + num, 0);
})
我还更新了计算逻辑以使用Array#reduce
,并使用带有隐式返回的箭头函数。
https://stackoverflow.com/questions/35842668
复制相似问题