I have an addon which uses a different host for a service in development and at runtime. I believed the solution to this would be to define the hostname in the config/environment.js of the application and to have a the service pick it up as follows:
import ENV from '../config/environment';
export default AjaxService.extend({
host: ENV.APP.serviceHost,
...
This however doesn't work as it believes that the path to the environment.js file is within the scope of the addon instead of the main application. What is the right way to do this when the name of the application in which this addon is included is not known?
I am using Ember 2.11.
I have an addon which uses a different host for a service in development and at runtime. I believed the solution to this would be to define the hostname in the config/environment.js of the application and to have a the service pick it up as follows:
import ENV from '../config/environment';
export default AjaxService.extend({
host: ENV.APP.serviceHost,
...
This however doesn't work as it believes that the path to the environment.js file is within the scope of the addon instead of the main application. What is the right way to do this when the name of the application in which this addon is included is not known?
I am using Ember 2.11.
Share Improve this question edited Feb 2, 2017 at 13:15 vogomatix asked Feb 2, 2017 at 12:46 vogomatixvogomatix 5,0912 gold badges26 silver badges49 bronze badges3 Answers
Reset to default 5I'm aware of 2 approaches for this:
Option 1
ember-config-service
This addon allows to access config via injected service. It solved the same issue for me.
Option 2
Manually pass config value via re-export.
// my-addon/app/services/my-ajax.js
// import config being in the app namespace
import ENV from '../config/environment';
import Service from 'my-addon/services/my-ajax';
export default Service.extend({
apiURL: ENV.APP.apiURL
});
/// my-addon/addon/services/my-ajax.js
export default AjaxService.extend({
init() {
console.log('api url in addon', this.apiUrl);
}
})
With recent versions of Ember, you can get the environment config from the container.
Ember.getOwner(this).resolveRegistration('config:environment')
I've used this package in my projects to good effect:
https://github./null-null-null/ember-get-config