具有余烬功率的Ember数组选项选择

时间:2016-12-28 20:51:32

标签: ember.js

我的模型有2个属性(baseCurrencyotherCurrencies),其值以2 ember power choices 下拉菜单的形式选择,其选项取决于2个计算属性( baseCurrencyOptionsotherCurrencyOptions

控制器

  ...
  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]

1 个答案:

答案 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);
        }
    });
});
  1. 定义像[{id:1,name:'Dollar',type:'base'},{id:2,name:'Yen',type:'other'}]这样的货币对象数组 这样您就可以使用计算宏来获取baseCurrenciesotherCurrencies
  2.     baseCurrencies:Ember.computed.filterBy('allCurrencies','type','base')
        otherCurrencies:Ember.computed.filterBy('allCurrencies','type','other')