Access the host address of the RESTAdapter programmatically

拥有回忆 提交于 2019-12-11 04:48:02

问题


I would like to obtain the host address of the REST adapter in a controller or in a component.

I'm using Ember-CLI, and I set up the adapter as follows:

export default DS.RESTAdapter.extend({
  host: 'http://localhost:9000'
});

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.

I tried all of the following:

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!

EDIT: 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.


回答1:


App.__container__.lookup('adapter:application').host

Example: 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
});

As you see, you can just grab it using App.applicationAdapterHost whenever you want, easily

http://emberjs.jsbin.com/OxIDiVU/979/edit



来源:https://stackoverflow.com/questions/25496826/access-the-host-address-of-the-restadapter-programmatically

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