Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4828

Service Property is Updated, Computed Property is Not

$
0
0

@Mofungo wrote:

I have a service on which I change a property ‘loggedIn’ from false to true. In the template, when I access ‘loggedIn’ directly from the service, I see the new value. When I access it through a computed property on my controller, I get the old value.

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() {
        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 --}}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4828

Trending Articles