funkensturm / ember-local-storage

The addon provides a storageFor computed property that returns a proxy and persists the changes to localStorage or sessionStorage. It ships with an ember-data adapter.
https://www.funkensturm.com/ember-local-storage/
MIT License
218 stars 76 forks source link

Use in an initialiser #270

Closed eharrow closed 6 years ago

eharrow commented 6 years ago

I wish to read from a storage object in an initialiser and then do something for the service being initialised. This is to support I18N where a user saved their preferred locale to local-storage and on the next app start the value will be read and the i18n add-on initiliased with it.

The issue I have is that using storageFor() in the way the docs work in an initialiser ends up with an error to do with the storage factory. How can I look up the storage instance in this case?

fsmanuel commented 6 years ago

@eharrow can't you use it in the service itself? Why do you need to do it in an initializer?

eharrow commented 6 years ago

ember-18n add-on exposes itself as a service and suggests initialising in an instance initialiser see https://github.com/jamesarosen/ember-i18n/wiki/Doc:-Setting-the-Locale

thx

fsmanuel commented 6 years ago

Ah I see. In this case I would suggest that you use the first approach in the application route:

// app/routes/application.js

export default Ember.Route.extend({
  i18n: Ember.inject.service(),
  userSettings: storageFor('userSettings'),

  // You can use beforeModel as you don't depend on the model hook
  beforeModel: function() {
    this.set('i18n.locale', this.get('userSettings.locale'));
  }
});
eharrow commented 6 years ago

Yes that works fine. Ta