将依赖项注入 Ember 模型 [英] Injecting dependencies into an Ember model
问题描述
我正在尝试将依赖项注入到我的 Ember 模型中.
I'm attempting to inject dependencies into my Ember models.
https://github.com/emberjs/ember.js/issues/3670,表明这是禁用的.按照链接到 https://github.com/stefanpenner/ember-cli/blob/master/blueprint/app/app.js#L4 演示了如何启用 MODEL_FACTORY_INJECTIONS
,理论上应该允许我将依赖项注入到我的模型中,但是我运气不好.
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.
在没有这个解决方案的情况下,除了将其转储到应用程序的命名空间(即 App.ImAGlobalconfig`)之外,还有其他方法可以将应用程序范围内的全局单例对象的引用注入到 Ember.Model 中吗?
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);
}
);
}
});
感谢所有帮助/意见.
推荐答案
模型注入确实有效:
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屋!