-
-
Save juggy/4c68453ce07c471560ca to your computer and use it in GitHub Desktop.
ember-with-custom-events
This file contains hidden or 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'; | |
class ItemRating extends HTMLElement { | |
createdCallback () { | |
this.insertAdjacentHTML('afterbegin', ` | |
<a data-rating="1">★</a> | |
<a data-rating="2">★</a> | |
<a data-rating="3">★</a> | |
<a data-rating="4">★</a> | |
<a data-rating="5">★</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); | |
} | |
} | |
}); |
This file contains hidden or 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.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