Question

I'm trying to built some authentication within my EmberJS application, so after the user has logged in, i save the user id and a token using local storage. This works as planned, i have then build a set up computed properties for helping determine if a players has logged in.

MyApp.ApplicationController = Ember.Controller.extend

  currentUser: (->
    @AuthManager.get('apiKey.user')
  ).property('AuthManager.apiKey')

  isAuthenticated: (->
    @AuthManager.isAuthenticated
  ).property('AuthManager.apiKey')

As you can see it just fetch the data from my AuthManager. Now my big problem is that these computed properties does not update, even tho the apiKey changes. So i tried to debug it.

  currentUser: (->
    @AuthManager.get('apiKey.user')
  ).property('AuthManager.apiKey')

  isAuthenticated: (->
    #@AuthManager.isAuthenticated
    console.log "Something changed to " + @get('AuthManager.apiKey.accessToken')

    self = @
    setInterval ->
      console.log self.get('AuthManager.apiKey.accessToken')
    , 1000
  ).property('AuthManager.apiKey.accessToken')

First the apiKey is undefined, but after the user is loaded, the apiKeys is set, but property fails to see this change.

This is the output of the code.

enter image description here

As you can see the apiKeys changes, but "Something changed to" is only printed once?

What am i doing wrong?

Was it helpful?

Solution

I think your problem is because the AuthManager starts with a capital letter, when you use @set('AuthManager.accessToken', ...) ember lookup the a global path, like window.AuthManager.accessToken instead of controllerInstance.AuthManager.accessToken so refactor your code to use just authManager.

setInterval trigger several times, because it's a timer that executes repeatedly until clearInterval is called, with the returned id from setInterval

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top