Skip to content

Instantly share code, notes, and snippets.

@pswai
Last active September 18, 2017 08:30
Show Gist options
  • Save pswai/8d846fdc435a400ce3980e06528a9347 to your computer and use it in GitHub Desktop.
Save pswai/8d846fdc435a400ce3980e06528a9347 to your computer and use it in GitHub Desktop.
Ember Event Sequence
import Ember from 'ember';
export default Ember.Component.extend({
disabled: false,
click() {
if (!this.get('disabled')) {
console.log('stop-ember-bubble.js: '.padStart(25), 'Ember `click` event stopped');
return false;
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
on: 'click',
disabled: false,
didInsertElement() {
this._super(...arguments);
if (!this.get('disabled')) {
Ember.$(this.get('element'))
.on(this.get('on'), e => {
console.log('stop-native-bubble.js: '.padStart(25), 'Native `click` event stopped');
e.stopPropagation();
});
}
},
willDestroyElement() {
Ember.$(this.get('element'))
.off(this.get('on'), e => e.stopPropagation());
}
});
import Ember from 'ember';
export default Ember.Component.extend({
click() {
console.log('x-bar.js: '.padStart(25), '`click`, component-level');
},
actions: {
handleNative() {
console.log('x-bar.js: '.padStart(25), '`handleNative`, hooked up with `onclick`');
},
handleEmber() {
console.log('x-bar.js: '.padStart(25), '`handleEmber`, hooked up with just `{{action}}`');
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
click() {
console.log('x-foo.js: '.padStart(25), '`click`, component-level');
},
actions: {
handleNative() {
console.log('x-foo.js: '.padStart(25), '`handleNative`, hooked up with `onclick`');
},
handleEmber() {
console.log('x-foo.js: '.padStart(25), '`handleEmber`, hooked up with just `{{action}}`');
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
handleNative() {
console.log('application.js: '.padStart(25), '`handleNative`, hooked up with `onclick`');
},
handleEmber() {
console.log('application.js: '.padStart(25), '`handleEmber`, hooked up with just `{{action}}`');
}
}
});
<p>
About this example:
<ul>
<li>Native events: e.g. <code>&lt;div onclick=\{{action 'theAction'}}&gt;&lt;/div&gt;</code></li>
<li>Ember events: e.g. <code>&lt;div \{{action 'theAction'}}&gt;&lt;/div&gt;</code></li>
<li>Ember events are executed after Native events</li>
<li>Stopping the bubbling of Native events stops all Ember events</li>
<li>Stopping the bubbling of Ember events does not stop Native events</li>
<li>You can play around with <code>x-bar.hbs</code> to have a feel of the sequence</li>
</ul>
</p>
<p>
View hierarchy: <code>Application Controller => x-bar => x-foo</code>
</p>
<div onclick={{action 'handleNative'}} {{action 'handleEmber'}}>
{{x-bar}}
</div>
{{#stop-native-bubble disabled=1}}
{{#stop-ember-bubble disabled=1}}
<div onclick={{action 'handleNative'}} {{action 'handleEmber' bubbles=true}}>
{{x-foo}}
</div>
{{/stop-ember-bubble}}
{{/stop-native-bubble}}
{{#stop-native-bubble disabled=true}}
<div onclick={{action 'handleNative'}} {{action 'handleEmber' bubbles=true}}>
Click Me and read console!
</div>
{{/stop-native-bubble}}
{
"version": "0.12.1",
"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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment