Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update view manually in Ember.js?

I am using Ember.js in my application, but there is a point where I update a property of the view's context(controller) but right after the update there is a parser(MathJax) that looks at the dom for the updated fields to parse it into math. However even though the update is taking place, it happens after mathjax looks for the update. What I need to do is force ember to update the view or wait for ember to update before I tell mathjax to parse the html. Is there a way to achieve this?

like image 438
JustKash Avatar asked Jan 06 '13 22:01

JustKash


People also ask

How do I update ember?

To update your Ember Mug, please refer to the settings section of the Ember App. From there, you can select to update the mug if an update is available. When there is an update available you will see a notification upon opening the app. You will have an option to install now (recommended) or install later.

Is Ember js a frontend?

Ember. js is an opinionated frontend JavaScript framework that has been getting a lot of interest lately.

Is Ember js MVC?

Ember. js is a MVC (Model-View-Controller) JavaScript framework used to develop large client-side web applications. In comparison to other JavaScript MVC frameworks, it gives you the ability to write organized and structured code.

What is Ember CLI build js?

Ember CLI, Ember's command line interface, provides a standard project structure, a set of development tools, and an addon system. This allows Ember developers to focus on building apps rather than building the support structures that make them run.


1 Answers

This is a fairly common use-case. To specify code that should execute after a property's changes have propogated, use an observer. Ember triggers observers after it successfully propagates the change. For example:

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')
});

See the Ember Managing-Asynchrony Guide and API docs for Ember.run: http://emberjs.com/guides/understanding-ember/managing-asynchrony/ http://emberjs.com/api/classes/Ember.run.html#method_next

like image 195
Mike Grassotti Avatar answered Oct 18 '22 13:10

Mike Grassotti