Application-wide data such as currency and locale


#1

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 http://stackoverflow.com/questions/21318130/global-variable-in-ember-js-with-local-storage which I could do, but it feels rather strange to have to change all controllers for that.

Any tips or pointers are warmly welcomed.


#2

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');
	}
});