mardi 7 juillet 2015

Is there anyway to call on a service that has dependencies on other services within an initializer in Ember v1.11?

I don't know if this is possible or not, but within an initializer I need to be able to call on a userPreferences service to fetch the user's saved locale before performing the fetch for the localized .json file. Unfortunately, with what I currently have the preference service method is called, but is unable to call a method within the api service contained within it.

Is there a better way of doing this?

// initializer
import Ember from 'ember';

export function initialize(container, application) {

  // need to obtain the user prefereces with an existing service
  var UserPrefs = container.resolver('service:user-preferences');
  var prefs = new UserPrefs();

  prefs.fetch().then(function () {
    console.log('FETch was done!!');
  });

  // fetch locale .json file here
}

// userPreferences service
import Ember from 'ember';

export default Ember.Service.extend({

  session: Ember.inject.service(),
  api: Ember.inject.service(),

  _cachedPrefs: null,

  fetch: function () {
    var self = this;

    // ERROR: unable to get an api reference here
    // => Assertion Failed: Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.
    var api = this.get('api');
    var userId = this.get('session.user.id');
    var cachedPrefs = this.get('_cachedPrefs');

    // ...
  }
}




Aucun commentaire:

Enregistrer un commentaire