0

こんにちは皆さん私はモデルを持っていますCurrency。フィールドname:string、がありますdefault:boolean。私のデータベースでは、デフォルト値を持つことができるレコードは 1 つだけで、このレコードを選択タグで選択したいと考えています。

例:

name: Eur default:false

name: USD default: true

name: RUR default: false

私がしたい:

<selected>
  <option>Eur</option
  <option selected=selected>USD</option
  <option>RUR</option
</selected>

Route.js

EmberMoney.IncomesRoute = Ember.Route.extend
  model: ->
    EmberMoney.Income.find()
  setupController: (controller) ->
    controller.set('currencies', EmberMoney.Currency.find());

収入.ハンドルバー

// Some output with Incomes records

{{view Ember.Select
       contentBinding="controller.currencies"
       optionLabelPath="content.name"
       optionValuePath="content.id"}}
4

1 に答える 1

1

次のようにサブクラス化Ember.Selectおよびオーバーライドできます。selection

EmberMoney.Select = Ember.Select.extend({
    selection: Ember.computed(function (key) {
      var content = this.get('content');
      if (!content || !content.length) return null;

      return content.findProperty('default', true)
    }).property('content.[]')
});

selectionサブクラスにはパラメーターがないためvalue、選択が変更されるとすぐに、そのインスタンスの計算されたプロパティが完全に置き換えられます。

のバインディングを設定するとselectionselectionはほとんどすぐにオーバーライドされ、代わりにソース オブジェクトでこのプロパティを定義するか、より複雑にする必要があることに注意してください。

EmberMoney.Select = Ember.Select.extend({
    selection: Ember.computed(function (key, value) {
      if (value === undefined || Ember.isNone(value)) {
          var content = this.get('content');
          if (!content || !content.length) return null;

          return content.findProperty('default', true)
      } else {
        return value;
      }
    }).property('content.[]')
});
于 2013-03-25T16:28:22.177 に答える