1

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.

Community
  • 1
  • 1
PeterParameter
  • 524
  • 5
  • 16

1 Answers1

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

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • 1
    BTW I agree using a common reference to a constant value both in the initialization of the adapter and when accessing it from elsewhere is the best option. I wonder why I haven't thought of that? :) – PeterParameter Aug 27 '14 at 20:03
  • for some reason the headers property in my ember component ApplicationAdapter(imported at the top via ES6) is undefined **ApplicationAdapter.prototype.headers** – FutuToad Dec 06 '14 at 21:21
  • Will you create a new question with all of your information? – Kingpin2k Dec 06 '14 at 21:43