2013-10-07 150 views
1

我有一個模板的標準Ember視圖。dmber中的Ember事件渲染屬性

模板:

<script type="text/html" data-template-name="template"> 
    <div id="container"> 
    <p>{{{view.description}}}</p> 
    </div> 
</script> 

查看:

Em.View.extend({ 
    templateName: 'OutlookSnapshotInformationTemplate', 
    description : "Loooooong text...", 
    descriptionChanged : function(){ 
    $('container').height() 
    [...] 
    }.observes('description') 
}), 

我想容器 div的尺寸,使用JQuery使用descriptionChanged觀察者。

問題是描述值是異步的,並在頁面呈現之前更新。

在DOM中重新呈現模板中的特定屬性時會引發一個事件嗎?

回答

1

在觀察者的內部,與Ember.run.scheduleOnce()的調用內部的DOM交互。

http://emberjs.com/api/classes/Ember.run.html#method_scheduleOnce

這確保了給定的代碼運行的渲染隊列被刷新後,並進一步指出,如果你碰巧在當前運行的循環更新說明多次,它將只運行一次:

Ember.run.scheduleOnce('afterRender', this, function() { 
    var height = this.$('#container').height(); 
    // ... 
    // profit! 
}); 
+0

完美地工作! – RedBass

0

使用您的視圖的didInsertElement方法:

App.YourView = Em.View.extend({ 
    templateName: 'OutlookSnapshotInformationTemplate', 
    description : "Loooooong text...", 
    didInsertElement: function() { 
    this.set('containerHeight', this.$('#container').height()); 
    console.log(this.get('containerHeight')); 
    } 
}), 
+0

是的,我知道* didInsertElement *,但在視圖的lifeCycle中只調用一次,並且在可視化過程中描述可能會改變很少的時間。 – RedBass

+0

是的,但你仍然可以保持你的觀察者。這個想法是在'didInsertElement'中設置視圖的'containerHeight'屬性,以便觀察者可以訪問它。 – chopper

+0

但問題在於,視圖呈現時或模型中的「說明」更改時,高度可能不正確。 只有當它在DOM中發生變化時纔是正確的 – RedBass