ember寄存器注入单例 [英] ember register inject singleton

查看:101
本文介绍了ember寄存器注入单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试更好地了解ember的注册和注册。
基本上我的代码看起来像这样:

  //创建一个服务
Nerdeez.Wormhole = Ember。 Object.extend({
...
})

//将此服务注册为单例
App.register('wormhole:current',Nerdeez。蠕虫,{singleton:true});

//想要在我的应用程序中注入此服务到处可用
//特别是在适配器中,我在ajax钩子中使用它
App.inject('App ','wormhole','wormhole:current');

//尝试在适配器中的ajax钩子中访问此服务时,我得到未定义的WTF!
App.get('wormhole')=== undefined // true

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



提前感谢

解决方案

我找不到将对象注入到应用程序中的方式,但您可以将其注入适配器(或存储),就像您可以使用路由和控制器对象:

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

//注入适配器
App.inject('adapter','wormhole','wormhole:current');

//注入商店
App.inject('store','wormhole','wormhole:current');

//注入路线&控制器
App.inject('route','wormhole','wormhole:current');
App.inject('controller','wormhole','wormhole:current');


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

解决方案

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

这篇关于ember寄存器注入单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆