如何将依赖注入到商店的适配器中

时间:2013-11-08 08:42:06

标签: ember.js ember-data

我使用ember-data,我需要将依赖项插入到商店的数据适配器中。这是简化的代码:

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 

1 个答案:

答案 0 :(得分:0)

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

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

您可以在my answerember register inject singleton中看到更多注入示例。