Ember.js:如何将依赖注入适配器?
我试图将一些全局应用程序配置(或状态或选项)注入所有组件,如路由、控制器和数据适配器。 不知道为什么这段代码不向数据适配器注入依赖项。也许问题出在订购上Ember.js:如何将依赖注入适配器?,ember.js,dependency-injection,Ember.js,Dependency Injection,我试图将一些全局应用程序配置(或状态或选项)注入所有组件,如路由、控制器和数据适配器。 不知道为什么这段代码不向数据适配器注入依赖项。也许问题出在订购上 var App = Ember.Application.create({ LOG_TRANSITIONS: true, LOG_VIEW_LOOKUPS: true }); var RESTAdapter = DS.RESTAdapter.extend({ primaryKey: '_id', buildU
var App = Ember.Application.create({
LOG_TRANSITIONS: true,
LOG_VIEW_LOOKUPS: true
});
var RESTAdapter = DS.RESTAdapter.extend({
primaryKey: '_id',
buildURL: function() {
var locale = this.get('options.locale');
return locale + this._super.apply(this, arguments);
}
});
App.ApplicationStore = DS.Store.extend({
adapter: RESTAdapter
});
App.initializer({
name: 'options',
initialize: function(container, app) {
var o = Ember.Object.create();
o.set('default-locale', 'en');
app.register('globals:options', o, { instantiate: false, singleton: true });
}
});
App.initializer({
name: 'injectOptions',
before: 'options',
initialize: function(container, app) {
app.inject('controller', 'options', 'globals:options'); // OK
app.inject('route', 'options', 'globals:options'); // OK
app.inject('data-adapter', 'options', 'globals:options'); // not working
app.inject('dataAdapter', 'options', 'globals:options'); // not working
app.inject('adapter', 'options', 'globals:options'); // not working
app.inject('store', 'options', 'globals:options'); // ???
}
});
注入是在创建时完成的,很可能存储和适配器只是在注入发生之前创建的。只需将初始值设定项设置为在余烬数据之前
App.initializer({
before:'ember-data',
name: 'options',
initialize: function(container, app) {
var o = Ember.Object.create();
o.set('default-locale', 'en');
app.register('globals:options', o, { instantiate: false, singleton: true });
}
});
App.initializer({
name: 'injectOptions',
before: 'options',
initialize: function(container, app) {
app.inject('controller', 'options', 'globals:options'); // OK
app.inject('route', 'options', 'globals:options'); // OK
app.inject('data-adapter', 'options', 'globals:options'); // not working
app.inject('dataAdapter', 'options', 'globals:options'); // not working
app.inject('adapter', 'options', 'globals:options'); // not working
app.inject('store', 'options', 'globals:options'); // ???
}
});
例如:
另外,声明存储是一种不推荐的模式。您只需声明适配器:
var RESTAdapter = DS.RESTAdapter.extend({
primaryKey: '_id',
buildURL: function() {
var locale = this.get('options.locale');
return locale + this._super.apply(this, arguments);
}
});
App.ApplicationAdapter = RESTAdapter;