2013-09-23 3 views
0

약속을 전달하는 서비스 내부의 동작을 이해하는 데 몇 가지 문제가 있습니다. 다음 코드에서는 하나의 비동기 호출 서버가 있습니다. .then 호출에서 @ $ rootScope를 사용하려고 시도 할 수 없습니다. 그러나 클래스 _rs의 속성에 @ $ rootScope를 할당하면 트릭을 수행합니다. 왜 그런지 모르겠다.CoffeeScript 클래스의 AngularJS 처리 약속

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 
) 

또한 getCurrentUser 메소드에서 _this.currentUser 대신 @currentUser를 사용할 때 작동하지 않습니다. 컨트롤러의 UI가 업데이트되지 않습니다.

도움 주셔서 감사합니다.

+0

좋아 자신에 의해 그것을 발견 : http://stackoverflow.com/questions/8965855/coffeescript-when-to-use-fat-arrow-over-arrow-and-vice-versa * DOH * – Alebon

+0

나는 서비스 객체의 속성에 대한 현재 범위에서 $ watch를 사용하여 접근 할 것이라고 생각한다. – jcollum

답변

0
namespace('com.myapp.service' 

    SessionService: 

    class SessionService 

     # Specify expected constructor services 
     @$inject : ["$rootScope", "$log", "User" ] 

     constructor : (@$rootScope, $log, @User) -> 

     $log.debug("Calling constructor of SessionService") 
     @currentUser = "" 

     @initCurrentUser() 

     return this 

     loadCurrentUser :() -> 
     return serverFunctions().func('sessionService').loadCurrentUser("async") 

     initCurrentUser :() => 
     result = @loadCurrentUser() 

     result.then (res) => 
      @$rootScope.$apply => 
      @currentUser = "test" 

     result.fail (e) => 
      @$rootScope.$apply => 
      console.error(e) 

     # Returns current user 
     getCurrentUser:() => 
     return @currentUser 
)