2013-06-25 53 views
8

我有一個Ember.Select視圖,我將如何將禁用的屬性添加/綁定到選項標記(而不是整個選擇框)。如何將「已禁用」屬性添加到Ember.Select中的選項

即,我很想完成以下結果。

<select> 
    <option value="1" disabled>I am a disabled option</option> 
    <option value="2">Im selectable</option> 
</select> 
+0

這是否有幫助..http://stackoverflow.com/questions/11457206/emberjs-how-to-disable-ember-select – PSL

+0

不幸的是,這將禁用整個選擇框。我想要禁用選擇框中的一些選項。 – jennas

回答

13

Ember.Select視圖不這樣做開箱。您需要爲disabled添加一個自定義屬性綁定,並添加相應的計算屬性以告訴Ember如何找到它。

一種簡單的方法是在用於呈現選擇的內容/數據項目上添加禁用屬性。

App.ApplicationController = Ember.Controller.extend({ 
    choices: function() { 
    return [ 
     Ember.Object.create({firstName: "Lorem", id: 1}), 
     Ember.Object.create({firstName: "Ipsum", id: 2, disabled: true}), 
     Ember.Object.create({firstName: "Dolor", id: 3}), 
     Ember.Object.create({firstName: "Sit", id: 4}), 
     Ember.Object.create({firstName: "Amet", id: 5}) 
    ]; 
    }.property() 
}); 

並重新打開或延長Ember.SelectOption視圖添加disabled屬性和計算屬性。

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

    disabled: function() { 
    var content = this.get('content'); 
    return content.disabled || false; 
    }.property('content'), 

}); 

這裏是一個工作jsbin。請注意,ipsum選項已禁用。

+0

謝謝。希望這有效地進入核心有一天;) – jennas

+0

我喜歡這種方法,但任何想法如何傳遞數據屬性,需要連字符名? – sbauch

+0

我用'.property('content.disabled')'而不是.'property('content')',但是否則這個效果很好。 – sblair

2

您可以處理它didInsertElement鉤在您的自定義視圖Ember.Select ..

didInsertElement: function() { 
    this.$('option[value="1"]').attr('disabled', true); 
} 
+1

不是真正的灰燼方式。您應該將綁定handlebars屬性改爲您的數據。 –

相關問題