以编程方式访问RESTAdapter的主机地址 [英] Access the host address of the RESTAdapter programmatically
问题描述
我想在控制器或组件中获取REST适配器的主机地址。
I would like to obtain the host address of the REST adapter in a controller or in a component.
我正在使用Ember-CLI,并按如下所示设置适配器:
I'm using Ember-CLI, and I set up the adapter as follows:
export default DS.RESTAdapter.extend({
host: 'http://localhost:9000'
});
我知道这个问题已被问到这里和这里,但这些方法中没有一个在最近的Ember 1.6.0中有效。
I am aware that this question has been asked here and here, but none of those approaches work in the recent Ember 1.6.0.
我尝试过以下内容:
DS.RESTAdapter.prototype.url
DS.RESTAdapter.prototype.host
App.__container__.lookup('store:main').get('adapter.url')
App.__container__.lookup('store:main').get('adapter.host')
DS.defaultStore.adapter.url
DS.defaultStore.adapter.host
有没有无论如何(无论多么肮脏/恶心/恶心)这样做?谢谢!
Is there any way whatsoever (no matter how dirty/hacky/nauseating) to do this? Thanks!
编辑:正确的答案是使用您可以从其他地方访问的值初始化适配器,如 Kingpin2k 下面指出。我最终创建了一个具有常量值的对象,并在这两种情况下引用它。
The correct answer is to initialize the adapter with a value you can access from elsewhere, like Kingpin2k pointed out below. I ended up creating an object with constant values and refer to it in both cases.
推荐答案
__container __。lookup('adapter:application')。host
示例: http://emberjs.jsbin.com/OxIDiVU/978/edit
老实说你应该只需将其定义为您的应用程序的属性,并在适配器中使用该属性,那么您可以通过这种方式将其从该应用程序中抓取。
Honestly you should just define it as a property on your app, and use that property in your adapter as well, that way you can grab it off the app w/o having to do that.
App = Ember.Application.create({
applicationAdapterHost: '/foo'
});
App.ApplicationAdapter= DS.RESTAdapter.extend({
host:App.applicationAdapterHost
});
如您所见,您可以使用 App.applicationAdapterHost
每当你想要,轻松地
As you see, you can just grab it using App.applicationAdapterHost
whenever you want, easily
http://emberjs.jsbin.com/OxIDiVU/979/edit
这篇关于以编程方式访问RESTAdapter的主机地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!