我的模型有2个属性(baseCurrency
和otherCurrencies
),其值以2 ember power choices 下拉菜单的形式选择,其选项取决于2个计算属性( baseCurrencyOptions
和otherCurrencyOptions
)
控制器
...
otherCurrencyOptions: Ember.computed('model.baseCurrency', function() {
console.log(`allCurrencies changed to: ${this.get('allCurrencies')}`);
return Ember.A(this.get('allCurrencies')).removeObject(this.get('model.baseCurrency'));
}),
baseCurrencyOptions: Ember.computed('model.otherCurrencies', function() {
console.log(`allCurrencies changed to: ${this.get('allCurrencies')}`);
return Ember.A(this.get('allCurrencies')).removeObjects(this.get('model.otherCurrencies'));
})
...
allCurrencies
属性包含所有货币的列表,不应随时间变化。
模板
{{#power-select searchEnabled=true options=baseCurrencyOptions selected=model.baseCurrency onchange=(action (mut model.baseCurrency) as |currency|}}
{{currency}}
{{/power-select}}
{{#power-select-multiple searchEnabled=true options=otherCurrencyOptions selected=model.otherCurrencies onchange=(action (mut model.otherCurrencies)) as |currency|}}
{{currency}}
{{/power-select-multiple}}
问题在于,每当我在两个下拉菜单allCurrencies
中的一个或另一个中选择货币时,属性都会以不可逆的方式更新为每个选定的货币。我期望Ember.A()
创建一个全新的对象,而无需修改`allCurrencies!此外,我收到了以下警告:
DEPRECATION:您在单个渲染中修改了两次concatenatedTriggerClasses。这在Ember 1.x中是不可靠的,将在Ember 3.0中删除[弃用ID:ember-views.render-double-modify]
答案 0 :(得分:0)
Ember.A
不会创建全新的Array
,它只会实现Ember.Enumerables
和数组接口(reference)。
以下是我建议的选项,
1)要创建全新的数组,您可以使用toArray
方法。所以改变这个
Ember.A(this.get('allCurrencies'))
至this.get('allCurrencies').toArray()
2)另一种选择是,使用allCurrencies
迭代forEach
并在回调内部使用iterate baseCurrency来检查条件并填充otherCurrency数组。
var baseCurreny = this.get('model.baseCurrency');
var otherCurrency = [];
this.get('allCurrencies').forEach(function(item, index, self) {
/*I assume id as your unique property to identify object
if (!baseCurrency.findBy('id', item.id)) {
otherCurrency.pushObject(item);
}*/
//or if its plain string then
baseCurrency.forEach(function(baseItem) {
if (item !== baseItem) {
otherCurrency.pushObject(item);
}
});
});
[{id:1,name:'Dollar',type:'base'},{id:2,name:'Yen',type:'other'}]
这样的货币对象数组
这样您就可以使用计算宏来获取baseCurrencies
和otherCurrencies
baseCurrencies:Ember.computed.filterBy('allCurrencies','type','base') otherCurrencies:Ember.computed.filterBy('allCurrencies','type','other')