Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to inject store in service ember.js

Tags:

ember.js

I tried to inject the store from the initializer without success, so I use lookup on init in my service, but I don't like it too much, I would rather keep things separated and put the injection in the initializer...

Ember : 1.11.1

Ember Data : 1.0.0-beta.16.1

jQuery : 1.11.2

NOT WORKING:Initializer

//app/initializers/initializer-store.js
export default {
  name: 'initializer-store',
  after: 'store',

  initialize: function(container, application) {
    application.inject('service:mtg-level-service', 'store', 'store:main');
  }
};

WORKING:Service

//app/services/mtg-level-service.js
import Ember from 'ember';

export default Ember.Service.extend({
  availableIn: ['controllers', 'routes'],
  store: null,

  init: function() {
    this._super();
    this.set('store', this.container.lookup("store:main"));
  }
});
like image 890
Alexandre Mélard Avatar asked Apr 26 '15 18:04

Alexandre Mélard


1 Answers

As of Ember v1.10:

import Ember from 'ember';

export default Ember.Service.extend({  
  store: Ember.inject.service('store')
});
like image 155
aceofspades Avatar answered Oct 11 '22 01:10

aceofspades