ember register注入singleton

时间:2013-06-14 17:48:25

标签: ember.js singleton

试图更好地理解ember的寄存器和注入。 基本上我的代码看起来像这样:

//creating a service
Nerdeez.Wormhole = Ember.Object.extend({
...
})

//registering this service as a singleton
App.register('wormhole:current', Nerdeez.Wormhole, {singleton: true});

//wanting to inject this service to be available everywhere in my application
//especially in the adapter where i use it in the ajax hook
App.inject('App', 'wormhole', 'wormhole:current');

//when trying to access this service in the ajax hook in the adapter i get undefined WTF!
App.get('wormhole') === undefined //true

我只是希望这个服务在整个应用程序中作为单例全局可用,实现这一目标的最佳方法是什么? 重要的是,我设法将我的服务注入模型,视图和控制器,问题是将它注入我的适配器。

提前致谢

1 个答案:

答案 0 :(得分:0)

我无法找到将对象注入App的方法,但是你可以将它注入到适配器(或存储)中,就像你可以用于路由&控制器对象:

// register
App.register('wormhole:current', Nerdeez.Wormhole);

// inject into adapter
App.inject('adapter', 'wormhole', 'wormhole:current');

// inject into store
App.inject('store', 'wormhole', 'wormhole:current');

// inject into routes & controllers
App.inject('route', 'wormhole', 'wormhole:current');
App.inject('controller', 'wormhole', 'wormhole:current');