Created
October 20, 2015 00:12
-
-
Save alexdiliberto/61525bb2906aec8ba2fb to your computer and use it in GitHub Desktop.
Pattern for dispatching events from Services in Ember
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
// 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