ember register inject singleton

人盡茶涼 提交于 2019-12-11 03:46:51

问题


Trying to understand better about ember's register and inject. Basically my code looks like this:

//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

i just want this service available globally in the entire application as a singleton, what is the best way to achieve this? it's important to say that i manage to inject my service to models, views, and controllers and the problem is injecting it to my adapter.

Thanks in advance


回答1:


I couldn't figure out a way to inject the object into App either, but you can inject it into the adapter (or store) just like you can with for route & controller objects:

// 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');


来源:https://stackoverflow.com/questions/17114408/ember-register-inject-singleton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!