Hi all,
I’m writing an app which should be available in multiple languages (will look at the i18n plugins soon) and the prices displayed will be available in multiple currencies.
How would I go about saving this global information in a way that it’s accesible for all controllers. I’ve considered Global Variable in Ember.js with local storage - Stack Overflow which I could do, but it feels rather strange to have to change all controllers for that.
Any tips or pointers are warmly welcomed.
To answer my own question, I went with using intializers and injecting the language & currency into my controllers. http://ember.zone/ember-application-initializers/
Any feedback to the following piece is warmly welcomed.
Ember.Application.initializer({
name: "currency",
initialize: function(container, application) {
application.register('user:currency', {
_currency: 'EUR',
currency: function(c) {
if(c !== undefined && c !== null) {
this._currency = c;
}
return this._currency;
}
}, {instantiate: false, singleton: true});
application.inject('controller', 'currency', 'user:currency');
application.inject('route', 'currency', 'user:currency');
}
});