2

试图更好地了解 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

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

提前致谢

4

1 回答 1

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');
于 2013-11-05T23:32:06.443 回答