I have a service where I change a prop ‘loggedIn’ from false to true. When I access the service’s prop through a computed property on my controller, I get the old value.
When I access the prop directly from the service, I see the new value.
The computed property on the controller only runs once – at app start. Why isn’t this the computed prop updated when the value it references in the service is updated?
Anyone have any clues as to what I am missing? I’m running Ember 3.1.3
Service:
// services/session
export default Service.extend({
loggedIn: false,
login() {
this.set('loggedIn', true);
},
isLoggedIn() {
return this.get('loggedIn');
}
});
Controler:
export default Controller.extend({
session: computed(function() {
return getOwner(this).lookup('service:session');
}),
isLoggedIn: computed('session', function() {
console.log(" isLogggedIn"); // this is only called once (when app starts)
let session = this.get('session');
let isUserLoggedIn = session.isLoggedIn();
return isUserLoggedIn;
})
});
Route:
export default Route.extend({
session: computed(function() {
return getOwner(this).lookup('service:session');
}),
actions: {
loginUser() {
this.get('session').login();
}
}
});
Template:
<button class="button" {{action "loginUser"}}>Login</button>
{{!-- After user clicks "login" button, values are: --}}
<h2>{{isLoggedIn}}</h2> {{!-- false --}}
<h2>{{session.loggedIn}}</h2> {{!-- true --}}