Last active
February 11, 2018 14:25
-
-
Save kumkanillam/53c90cdf19ef345625fb4b22c7a2b232 to your computer and use it in GitHub Desktop.
Proper Teardown event handler when we use bind function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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