2013-01-06 14 views
8

我在我的應用程序中使用了Ember.js,但是有一點我更新了視圖的上下文(控制器)的屬性,但在更新後有一個解析器(MathJax)查看更新字段的dom把它解析成數學。但是,即使更新正在進行,它發生在mathjax查找更新之後。我需要做的是強制ember更新視圖或等待ember更新,然後再告訴mathjax解析html。有沒有辦法做到這一點?如何在Ember.js中手動更新視圖?

回答

17

這是一個相當常見的用例。要指定在屬性更改傳播後應執行的代碼,請使用觀察者。 Ember在成功傳播更改後觸發觀察者。例如:

App.MathView = Ember.View.extend({ 
    controller: null, 
    template: Ember.Handlebars.compile("<div>{{myProperty}}</div>"), 

    myPropertyDidChange: function() { 
    // From here the controller value is changed but DOM has not been updated 
    Ember.run.next(this, function() { 
     // This code will run when after changes have propagated to the DOM 
     // Call MathJax parser here 
     // If needed you can access the view's DOM element via this.$() 
     alert("New property value is in dom: "+ this.$().text()); 
    }); 
    }.observes('controller.myProperty') 
}); 

見灰燼管理,異步性指南和API文檔Ember.run: http://emberjs.com/guides/understanding-ember/managing-asynchrony/ http://emberjs.com/api/classes/Ember.run.html#method_next

+0

謝謝,我需要的是Em.run.next(this,function(){... – Akash

+0

現在視圖已經消失了。現在我應該在哪裏查看屬性更改? – Cameron