Skip to content

Instantly share code, notes, and snippets.

@alexdiliberto
Created October 20, 2015 00:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdiliberto/61525bb2906aec8ba2fb to your computer and use it in GitHub Desktop.
Save alexdiliberto/61525bb2906aec8ba2fb to your computer and use it in GitHub Desktop.
Pattern for dispatching events from Services in Ember
// app/services/locale.js
export default Ember.Service.extend(Ember.Evented, {
init() {
this._super(...arguments);
// initialize locales from localStorage (or URL string)
},
currentLocale() {
return ...;
},
changeLocale(newLocale) {
this.trigger('localeChanged', newLocale);
}
});
// app/components/my-component.js
import service from 'ember/injections';
import on from 'ember/decorators';
export default Ember.Component.extend({
locale: service('locale'),
init() {
this._super(...arguments);
this.currentLocale().foo;
},
on('locale.localeChanged', function(newLocale) {
// ...
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment