要使用colorselector插件,colorselector()
函數應在 在瀏覽器中使用選項呈現select後調用。但是,使用ember didInsertElement
不起作用,因爲在插入<option>
標籤之前觸發了回調!在插入子元素之前觸發didInsertElement事件
我嘗試這樣做:
App.ColorSelector = Ember.Select.extend({
_initialize: function() {
console.log(this.$().find('option').size()); // logs 0
this.$().colorselector();
}.on('didInsertElement');
});
App.ColorSelector = Ember.Select.extend({
_initialize: function() {
Ember.run.scheduleOnce('afterRender', this, function() {
console.log(this.$().find('option').size()); // logs 0
this.$().colorselector();
});
}.on('didInsertElement');
});
編輯: 使用@SeanK建議:
App.ColorSelector = Ember.Select.extend({
didInsertElement: function() {
console.log(this.$().find('option').size()); // logs 0
this.$().colorselector();
}
});
如何運行燼後colorselector()
函數調用插入<select>
所有<option>
標籤?
嘗試移動你的console.log調用是後您的來電colorselector()。您在插入前輸出大小。這會改變日誌輸出嗎? – SeanK 2014-10-02 15:08:07
不要更改@SeanK。問題是:當函數didInsertElement被調用時,選項尚未插入。我需要將'colorselector()'調用移至Ember後面插入所有
啊,我明白了,所以您在模板中的標籤上指定的內容尚未在DOM中。這很奇怪。你是如何設置你的內容數組的?我很好奇,如果給colorselector()調用添加延遲,會發生什麼情況。嘗試將它包含在Ember.run.later調用中,並延遲一段時間,比如說半秒,然後看看是否會改變事情。 也有可能在這種情況下Ember.Select視圖不是正確的選擇 - 它可能無法與Bootstrap之類的東西配合良好。您可以嘗試創建自己的自定義視圖或組件。 – SeanK 2014-10-02 15:33:30