Skip to content

Instantly share code, notes, and snippets.

@nishanmiranda
Last active March 1, 2016 11:20
Show Gist options
  • Save nishanmiranda/369a96cea1172acf1f2a to your computer and use it in GitHub Desktop.
Save nishanmiranda/369a96cea1172acf1f2a to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
Page 1 renders `my-component` which attaches `on-click` handlers to window.document. It appends .bind(this) to the callback function for getting component context in callback. But binding(this) creates a new instance of the function and it will not be the same while removing.
Hence the event handlers will not be removed on `willDestroy`.
<br >
<br >
So Everytime switching between page1 and page2, new event handlers will be registered (and will not be closed) and might lead to performance issues. Check Console.
<br >
<br >
{{#link-to "page1"}}Page1{{/link-to}}
{{#link-to "page2"}}Page2{{/link-to}}
<br>
<br>
{{outlet}}
<br>
<br>
import Ember from 'ember';
export default Ember.Component.extend({
callback() {
console.log(`document clicked and handler registered by ${this.elementId}`);
},
init() {
this._super(...arguments);
$(window.document).on('click', this.callback.bind(this));
},
willDestroy() {
this._super(...arguments); $(window.document).off('click',this.callback.bind(this));
}
});
import Ember from 'ember';
export default Ember.Route.extend({
});
{{#my-component}}
Component Rendered and click event registered to document
{{/my-component}}
<br>
<br>
{{#link-to "application"}}
Back to Home
{{/link-to}}
import Ember from 'ember';
export default Ember.Route.extend({
});
{{#link-to "application"}}
Back to Home
{{/link-to}}
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none'
});
Router.map(function() {
this.route('page1');
this.route('page2');
});
export default Router;
{
"version": "0.6.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "beta",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.3.3/ember-data.js",
"ember-template-compiler": "beta"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment