最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Injecting session into a model - Stack Overflow

programmeradmin2浏览0评论

I'd like to be able to inject my Session singleton into my Ember models. The use case I'm trying to support is having puted properties on the model that react to the user's profile (a property on 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');
    }
});

The injection works fine into the controller but I'm having trouble with getting it to the model. Is there any limitation here? Any special techniques?

-------- Additional Context ---------

Here's an example of what I'd like to be able to do in my model definition:

App.Product = DS.Model.extend({
    name: DS.attr("string"),
    pany: DS.attr("string"),
    categories: DS.attr("raw"),
    description: DS.attr("string"),

    isConfigured: function() {
        return this.session.currentUser.configuredProducts.contains(this.get('id'));
    }.property('id')
}); 

I'd like to be able to inject my Session singleton into my Ember models. The use case I'm trying to support is having puted properties on the model that react to the user's profile (a property on 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');
    }
});

The injection works fine into the controller but I'm having trouble with getting it to the model. Is there any limitation here? Any special techniques?

-------- Additional Context ---------

Here's an example of what I'd like to be able to do in my model definition:

App.Product = DS.Model.extend({
    name: DS.attr("string"),
    pany: DS.attr("string"),
    categories: DS.attr("raw"),
    description: DS.attr("string"),

    isConfigured: function() {
        return this.session.currentUser.configuredProducts.contains(this.get('id'));
    }.property('id')
}); 
Share Improve this question edited Dec 24, 2016 at 14:07 Marcio Junior 19.1k4 gold badges46 silver badges47 bronze badges asked Nov 15, 2013 at 9:14 kenken 9,04313 gold badges79 silver badges136 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

By default injections in models don't work. To do so 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 downside of this, is that it create some break changes:

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

  • productRecord.constructor === App.Product will evaluate to false. To solve this you can use App.Product.detect(productRecord.constructor).

发布评论

评论列表(0)

  1. 暂无评论