Embedding a session in a model

I would like to be able to inject singleton Session into my Ember models. In the case of the use that I am trying to support, there are computed model properties that respond to the user profile (property of the Session object).

 App = window.App = Ember.Application.create({ ready: function() { console.log('App ready'); this.register('session:current', App.Session, {singleton: true}); this.inject('session:current','store','store:main'); this.inject('controller','session','session:current'); this.inject('model','session','session:current'); } }); 

Injection works fine in the controller, but I'm having trouble getting it in model . Are there any limitations here? Any special methods?

-------- Additional context ---------

Here is an example of what I would like to do in my model definition:

 App.Product = DS.Model.extend({ name: DS.attr("string"), company: DS.attr("string"), categories: DS.attr("raw"), description: DS.attr("string"), isConfigured: function() { return this.session.currentUser.configuredProducts.contains(this.get('id')); }.property('id') }); 
0
javascript ember-data
source share
1 answer

By default, injections in models do not work. To do this, you need to set the flag Ember.MODEL_FACTORY_INJECTIONS = true :

 Ember.MODEL_FACTORY_INJECTIONS = true; App = window.App = Ember.Application.create({ ready: function() { console.log('App ready'); this.register('session:current', App.Session, {singleton: true}); this.inject('session:current','store','store:main'); this.inject('controller','session','session:current'); this.inject('model','session','session:current'); } }); 

The disadvantage of this is that it creates some break changes:

  • If you have App.Product.FIXTURES = [...] , you can use App.Product.reopenClass({ FIXTURES: [...] });

  • productRecord.constructor === App.Product will be evaluated to false . To solve this problem, you can use App.Product.detect(productRecord.constructor) .

+10
source share

All Articles