Skip to content

Instantly share code, notes, and snippets.

@patrickarlt
Last active January 6, 2017 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save patrickarlt/6f009294d78737f7f56e to your computer and use it in GitHub Desktop.
Save patrickarlt/6f009294d78737f7f56e to your computer and use it in GitHub Desktop.
ember-with-custom-events
import Ember from 'ember';
class ItemRating extends HTMLElement {
createdCallback () {
this.insertAdjacentHTML('afterbegin', `
<a data-rating="1">&#9733;</a>
<a data-rating="2">&#9733;</a>
<a data-rating="3">&#9733;</a>
<a data-rating="4">&#9733;</a>
<a data-rating="5">&#9733;</a>
`);
}
attachedCallback () {
this.addEventListener('click', (e) => {
var rating = parseInt(e.target.getAttribute('data-rating'));
var e = new CustomEvent('rateitem', {
bubbles: true,
details: {
rating: rating
}
});
this.dispatchEvent(e);
});
}
}
document.registerElement('item-rating', ItemRating);
export default Ember.Controller.extend({
actions: {
doSomethingWithRating: function (){
console.log('action fired from rateitem event with args', arguments);
}
}
});
<item-rating {{action 'doSomethingWithRating' on 'rateitem'}}></item-rating>
{
"version": "0.4.9",
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.9/ember.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/1.13.11/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.9/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment