Pass object from Appliction to all the routers and components

i just want my ember app to work based on an object send via arguments in Application.create(args).

Is it possible to get that object as property from application in controller or router (or) in the other way, can application pass that specific property to router and controller?

While there is no easy way to do this by default from a route or controller, there are services and you can inject properties using initializers.

A service is basically an Ember.Object that persists data across the app. You can use them for all sort of things, a great example would be session data. Put all the data you need here. http://emberjs.com/api/classes/Ember.Service.html

# /app/services/auth.js
export default Ember.Service.extend({
  isLoggedIn: false
});

Then use an initializer to inject the data wherever you need.

# /app/initializers/service-auth.js
export default {
  name: 'service-auth',
  initialize: function(container, application) {
    application.inject('route', 'auth', 'service:auth');
    application.inject('controller', 'auth', 'service:auth');
    application.inject('component', 'auth', 'service:auth');
  }
};

This is extremely useful but sometimes you really don’t want to inject it everywhere, if that’s the case don’t use an initializer and just inject it when you actually need it. Eg.

# /app/components/x-profile.js
export default Ember.Component.extend({
  auth: Ember.inject.service()
});
{{!-- /app/templates/components/x-profile.hbs --}}
{{#if auth.isLoggedIn}}
  <p>You are logged in!</p>
{{/if}}
1 Like