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?
I actually have similar code and it works~
radq
January 16, 2014, 7:33pm
3
willClearRender
is probably a better place to put your cleanup code.
I believe my problem was that my hook was willDestroy
, not willDestroyElement
.
@radq when is willClearRender
called? The docs say it’s for rerender .
radq
January 16, 2014, 9:39pm
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
On a related note, does anyone see any problems with this approach? Lifecycle Hooks in Ember.js Views :: madhatted.com
Notice the use of ‘willClearRender’ to clean up custom event handlers… agree this is a good practice?
I’m looking for best practices to create event handlers and then clean up.