如何将依赖项注入存储的适配器



我使用余烬数据,我需要将依赖项插入存储的数据适配器中。以下是简化的代码:

window.App = Ember.Application.create({
  ready: function() {
    this.register('database:current', this.Database);
     // this works fine, this.get('database') inside routes works ok
    this.inject('route', 'database', 'database:current');
    // but this does not work
    this.inject(App.SqliteAdapter, 'database', 'database:current');
    // also tried this:
    // this.inject('dataAdapter', 'database', 'database:current');
  }
});
App.Store = DS.Store.extend({
  revision: 13,
  adapter: App.SqliteAdapter
});
App.SqliteAdapter = DS.Adapter.extend({
  find: function(store, type, id) {
    var db = this.get('database');
    console.log(db); // this is undefined
  }
});
App.Database = Ember.Object.extend({});

为什么这段代码不起作用?

版本:

DEBUG: Ember      : 1.1.2
DEBUG: Ember Data : 1.0.0-beta.3
DEBUG: Handlebars : 1.0.0
DEBUG: jQuery     : 2.0.3 

您可以像这样注入适配器:

this.inject('adapter', 'database', 'database:current');

您可以在我对余烬寄存器注入单例的回答中看到更多注入示例。

最新更新