Skip to content

Instantly share code, notes, and snippets.

@kumkanillam
Last active February 11, 2018 14:25
Show Gist options
  • Save kumkanillam/53c90cdf19ef345625fb4b22c7a2b232 to your computer and use it in GitHub Desktop.
Save kumkanillam/53c90cdf19ef345625fb4b22c7a2b232 to your computer and use it in GitHub Desktop.
Proper Teardown event handler when we use bind function
import Ember from 'ember';
export default Ember.Component.extend({
exampleProperty: 'example value',
init() {
this._super(...arguments);
},
didInsertElement() {
this._super(...arguments);
//Ember.$('body').click(this.exampleMethod.bind(this)).click();
Ember.$('body').on('click',this.exampleMethod.bind(this));
},
willDestroyElement() {
this._super(...arguments);
},
exampleMethod() {
//Here this will refers to component,
console.log("ComponentProperty access as usual ",this.get('exampleProperty'));
},
actions:{
remove(){
//Ember.$('body').off('click'); //- this will remove all the eventsHandler assigned to body for click event.
Ember.$('body').off('click', this.exampleMethod);
console.log("This will not remove event handler, since we attached function different function which is returned by bind function.");
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
<br>
{{my-component }}
<br>
{{outlet}}
<br>
<br>
MyComponent
<button{{action 'remove'}}> Remove Event </button>
{{yield}}
{
"version": "0.13.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.16.2",
"ember-template-compiler": "2.16.2",
"ember-testing": "2.16.2"
},
"addons": {
"ember-data": "2.16.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment