我想包括一个计算标志在我的模型中,当一个特定的字符串存在于包含的数组中。从阵列的灰烬计算属性
userType: DS.attr(), // ['Don','Ben','Dec', 'Con']
isDon: Ember.computed('userType', function() {
return (!((this.get('userType')).includes('Don'));
}).property('userType'),
有帮助吗?
我想包括一个计算标志在我的模型中,当一个特定的字符串存在于包含的数组中。从阵列的灰烬计算属性
userType: DS.attr(), // ['Don','Ben','Dec', 'Con']
isDon: Ember.computed('userType', function() {
return (!((this.get('userType')).includes('Don'));
}).property('userType'),
有帮助吗?
您的语法错误。它看起来你混合定义计算性能的两种不同的方式,
isDon: Ember.computed('userType', function() {
return (!((this.get('userType')).includes('Don'));
}),
如果要重新计算isDon
每次当你添加/ userType
删除元素再考虑相关的密钥userType.[]
参考
https://guides.emberjs.com/v2.13.0/object-model/computed-properties/ https://guides.emberjs.com/v2.13.0/object-model/computed-properties-and-aggregate-data/#toc_code-code-vs-code-each-code
由于一些奇怪的原因,userType不被视为一个数组,所以我不得不创建一个计算的属性,如
userRoles: Ember.computed('userType.[]', function() {
return Ember.A(this.get('userType'));
}),
isDon: Ember.computed('userRoles', function() {
return this.get('userRoles').filter((item) => item == 'Don').length > 0;
}),
'.property()'让我想知道你是否在看老文档。你在什么版本上? – sheriffderek