Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind to a view re-render in Ember.js?

I have some jQuery code which needs to be run when a view is rendered. For the initial render I can easily do

App.FooView = Ember.View.extend({
  didInsertElement: function() {
    console.log("the view was rendered");
    this.$().someStuff();
  }
});

but since the someStuff() method is manipulating properties of the view elements, I need to run it when the view is re-rendered due to data bindings, because it will lose the custom generated properties.

like image 207
Jakub Arnold Avatar asked Dec 25 '12 18:12

Jakub Arnold


1 Answers

If you would like to re-render the view when a certain attribute changes then what you want would look something like this:

App.FooView = Ember.View.extend({
    myProperty: 'This value might change',
    myPropertyChanged: function() {
        //Do some stuff here (maybe change the template) and call...
        this.rerender();
    }.observes('myProperty')
});

But if you are looking to only re-render the view after the view is created then you want something more like this:

App.FooView = Ember.View.extend({
    init: function() {
        this._super();
        //Do some stuff here (maybe change the template) and call...
        this.rerender();
    }
});

SOURCE: http://emberjs.com/api/classes/Ember.View.html

like image 154
JustKash Avatar answered Oct 02 '22 13:10

JustKash