将依赖项注入到Ember模型中 [英] Injecting dependencies into an Ember model
问题描述
https://github.com/emberjs/ember.js/issues/3670 ,表示禁用此功能。连结到 https://github.com /stefanpenner/ember-cli/blob/master/blueprint/app/app.js#L4 演示了如何启用 MODEL_FACTORY_INJECTIONS
,理论上应该允许我在我的模型中注入依赖关系,但是我没有运气。
在没有这个解决方案的情况下,还有其他方法可以将引用注入到应用程序范围,全局单例对象成为一个Ember.Model,除了将其转储到应用程序的命名空间(即App.ImAGlobalconfig`)?
作为参考,这是初始化程序我' m工作在
App.initializer({
name:'preload',
initialize:function容器/ *,应用程序* /){
App.deferReadiness();
Ember。$。ajax({
url:CONFIG.configURL,
dataType: 'json',
context:this
})。done(
function(json / *,status,request * /){
var appConfig;
// ...
container.register('app:config',appConfig,{instantiate:false});
container.injection('controller','appConfig','app:config');
container.injection('route','appConfig','app:config');
container.injection('view','appConfig','app:config');
container.injection('model','appConfig','app:config'); //我不工作
App.advanceReadiness();
}
).fail(
函数(状态,请求,错误){
console.log('无法加载配置为:'+错误);
}
);
}
});
感谢任何和所有的帮助/意见。
模型注射确实有效:
Ember.MODEL_FACTORY_INJECTIONS = true;
App = Ember.Application.create();
App.initializer({
name:'model-injection',
initialize:function(container,application){
var cfg = {version:'1'};
container.register('app:config',cfg,{instantiate:false});
container.injection 'model','cfg','app:config');
App.advanceReadiness();
}
});
App.deferReadiness();
http://emberjs.jsbin.com/lomiy/1/edit
I'm attempting to inject dependencies into my Ember models.
https://github.com/emberjs/ember.js/issues/3670, states that this is disabled. Following a link to https://github.com/stefanpenner/ember-cli/blob/master/blueprint/app/app.js#L4 demonstrates how to enable MODEL_FACTORY_INJECTIONS
, which in theory, should allow me to inject dependencies into my models, however I'm having no luck.
In the absence of this solution, are there other ways to inject a ref to an app-wide, global singleton object into an Ember.Model besides just dumping it onto the app's namespace (ie. App.ImAGlobalconfig`)?
for reference, this is the initializer I'm working on
App.initializer({
name: 'preload',
initialize: function(container/*, application*/) {
App.deferReadiness();
Ember.$.ajax({
url: CONFIG.configURL,
dataType: 'json',
context: this
}).done(
function(json/*,status, request*/) {
var appConfig;
// ...
container.register('app:config', appConfig, {instantiate: false});
container.injection('controller', 'appConfig', 'app:config');
container.injection('route', 'appConfig', 'app:config');
container.injection('view', 'appConfig', 'app:config');
container.injection('model', 'appConfig', 'app:config'); // I don't work!
App.advanceReadiness();
}
).fail(
function(status, request, error) {
console.log('Unable to load config with: ' + error);
}
);
}
});
Thanks for any and all help/opinions.
Model injections does indeed work:
Ember.MODEL_FACTORY_INJECTIONS = true;
App = Ember.Application.create();
App.initializer({
name: 'model-injection',
initialize: function(container, application) {
var cfg = {version: '1'};
container.register('app:config', cfg, {instantiate: false});
container.injection('model', 'cfg', 'app:config');
App.advanceReadiness();
}
});
App.deferReadiness();
http://emberjs.jsbin.com/lomiy/1/edit
这篇关于将依赖项注入到Ember模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!