I have a ember-cli project named Car
,
I can access Car
object from window.Car, It seems ember-cli create a global object for me on development env.
But if I run serve build
, I am lost the Car
.
I will create a initializer and create the Car
manually if there is no better way.
I think this might be usful: GitHub - ember-cli/ember-export-application-global . But I’ve never used it so I don’t know.
In our app, we have an initializer (like you suggested) that does this work for us. It looks like this:
// app/initializers/app-setup.js
export default {
name: 'app-setup',
initialize: function(container, application) {
window.BC = application;
}
};
We use the initializer because we have more setup to do as well. We have data embedded by server into the HTML that needs to be configured on the application (CSRF for example).
1 Like
Thanks for you reply, I wanna why ember developers remove the global application instance when build.
I store some static data in the global instance, like api host url.
Am I lost the best practice?
Yea, generally speaking, you should not use the global instance within your app. So doing window.App.hostUrl
from inside the app (controller, component, adapter, etc) is a bad practice because it makes testing and other things more complicated. If you need to access hostUrl
put it in a “config” service and use config: Ember.inject.service()
where ever you need to access it.
You should only use window.App
for debugging or some setup that happens outside the scope of the app.
1 Like
You’re welcome! Happy Embering.
1 Like