如何在Ember.Select中为选项添加“已禁用”属性

时间:2014-05-30 14:31:24

标签: ember.js

我知道它不是开箱即用的,但我想知道什么是最简单的方法。

我在计算属性中有2个数组,一个有好的选项,另一个有“禁用的”。

如果不崩溃我的其他“正常”选择,我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

最简单的方法是将disabled绑定添加到Ember.SelectOption

Ember.SelectOption.reopen({
  attributeBindings: ['disabled'],

  init: function() {
    this.disabledPathDidChange();
    this._super();
  },

  disabledPathDidChange: Ember.observer('parentView.optionDisabledPath', function() {
    var valuePath = Ember.get(this, 'parentView.optionDisabledPath');

    if (!valuePath) { return; }

    Ember.defineProperty(this, 'disabled', Ember.computed(function() {
      return Ember.get(this, valuePath);
    }).property(valuePath));
  })
});

然后可以这样使用:

{{view Ember.Select content=model
                    optionLabelPath="content.id"
                    optionValuePath="content.id"
                    optionDisabledPath="content.disabled"}}

换句话说,您只需要在content的每个项目上都有一些属性,这些属性可以判断该选项是否应该被禁用。

http://emberjs.jsbin.com/lewepara/1/edit