ken*_*ken 0 javascript ember.js ember-data
我希望能够将我的Session单身注入我的Ember模型中.我试图支持的用例是在模型上计算了对用户配置文件作出反应的属性(Session对象上的属性).
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');
}
});
Run Code Online (Sandbox Code Playgroud)
注射可以很好地控制到控制器,但我遇到麻烦model.这里有限制吗?任何特殊技术?
--------附加背景---------
这是我希望能够在我的model定义中做的一个例子:
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')
});
Run Code Online (Sandbox Code Playgroud)
Mar*_*ior 11
默认情况下,模型中的注入不起作用.为此,您需要设置标志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');
}
});
Run Code Online (Sandbox Code Playgroud)
这样做的缺点是它会产生一些中断变化:
如果你有App.Product.FIXTURES = [...]需要使用App.Product.reopenClass({ FIXTURES: [...] });
productRecord.constructor === App.Product将评估为false.要解决这个问题,你可以使用App.Product.detect(productRecord.constructor).
| 归档时间: |
|
| 查看次数: |
1857 次 |
| 最近记录: |