What is the correct way to run JavaScript (in my case, jQuery) to change the DOM part (Ember view).
Use didInsertElementin my view does not start between route context changes. For instance. from one message to another.
App.PageView = Ember.View.extend({
didInsertElement: function() {
console.log(this.$().find('img').length);
}
});
Tried to use a hook afterRender, but the same result. I suppose this is because Ember only updates the Handlebars variables, not the look itself, since they are from the same model.
App.PageView = Ember.View.extend({
didInsertElement: function() {
Ember.run.scheduleOnce('afterRender', this, this.afterRenderEvent);
},
afterRenderEvent: function() {
console.log(this.$().find('img').length);
}
});
I believe this works for me, binding the observer to didInsertElement as follows:
App.PageView = Ember.View.extend({
didInsertElement: function() {
Ember.run.scheduleOnce('afterRender', this, this.afterRenderEvent);
}.observes('controller.images'),
afterRenderEvent: function() {
if (this.$()) {
console.log(this.$().find('img').length);
}
}
});
Observations listen to my “images” property on my model, which seems to work. Is this the right thing to do? Seems a little successful or what am I missing?
, .