I am having some problems to understand the behaviour inside a service handing promises. In the following code i have one async call the server. Trying to use @$rootScope in the .then call is not possible. But assigning @$rootScope to an attribute in the class _rs does the trick. And i don't get why?
namespace( 'com.myapp.service'
SessionService:
class SessionService
_rs = null
_this = null
# Specify expected constructor services
@$inject : ["$rootScope", "$log", "User" ]
constructor : (@$rootScope, $log, @User) ->
# Scope binding and this interpolation
_rs = @$rootScope
_this = @
$log.debug("Calling constructor of SessionService")
@currentUser = ""
@initCurrentUser()
return this
loadCurrentUser : () ->
return serverFunctions().func('sessionService').loadCurrentUser("async")
initCurrentUser : () ->
result = @loadCurrentUser()
result.then (res) ->
_rs.$apply ->
_this.currentUser = "test"
result.fail (e) ->
_rs.$apply ->
console.error(e)
# Returns current user
getCurrentUser: () ->
return _this.currentUser
)
Furthermore its not working when i use @currentUser instead of _this.currentUser in the getCurrentUser method. The UI in the controllers is just not updating.
Thanks for your help!