Search code examples
ember.jsember-cli

Can I have a computed property from an injected service?


I have an isAuthenticated property in a service that is currently injected on my application route, how can I have a computed property from the injected service in my route?

export default Ember.Route.extend({
    session : Ember.inject.service('market-session'),
    isUser : Ember.computed.oneWay('session.IsAuthenticated'),
}

Is this possible? In template doesn't seems to get the value.

Inside the template -

{{#if isUser}}
   User is authenticated
{{else}}
   User log in form
{{/if}}

This computed property only works if I move it to the controller, this should be working in both the route and controller right? Am I missing something here?


Solution

  • Once the service has been injected, you can access computed properties on the service exactly how you have it shown above. From the Ember docs:

    Creates a property that lazily looks up a service in the container. There are no restrictions as to what objects a service can be injected into.

    You can access both computed properties and call functions on the service object as you would expect.