2013-03-25 57 views
0

你好家伙我有型号Currency。我有字段name:string,default:boolean。在我的数据库中,只有一条记录可以有默认值,我希望在选择标签中选择这条记录。默认值在选择Ember js

实施例:

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()); 

incomes.handlebars

// Some output with Incomes records 

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

回答

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参数,计算出的属性将被永久尽快更换该实例选择被改变。

注意,如果你设置了一个用于selection结合,selection几乎会立即覆盖,你反而要定义此属性源对象或变得更加复杂:

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.[]') 
}); 
+0

感谢您的帮助但我不明白。在什么文件中最好放置这个扩展选择?我把它放在视图文件中。可以吗?这是呈现此选择的正确方法?当我在我的第一篇文章中呈现?这对我不起作用。对不起,愚蠢的问题。 – EJIqpEP7 2013-03-26 05:20:32