How to do DOM cleanup?


#1

In Ember 0.9, I have code that looks like

MyView = Em.View.extend({
  didInsertElement: function() {
    this.$().on('foo', this.bar.bind(this));
  },
  willDestroyElement: function() {
    this.$().off('foo');
  }
})

When I try to run that on Ember 1.0, it complains that this.$() is undefined in willDestroyElement. That’s because the view is already in the destroying state. Where am I supposed to put this cleanup code?


#2

I actually have similar code and it works~


#3

willClearRender is probably a better place to put your cleanup code.


#4

I believe my problem was that my hook was willDestroy, not willDestroyElement.

@radq when is willClearRender called? The docs say it’s for rerender.


#5

willClearRender is called when a view is about to be rerendered and also before willDestroyElement is triggered.

http://madhatted.com/2013/6/8/lifecycle-hooks-in-ember-js-views