javascript - Closure problems in class definition -


i'm using coffescript create angular application.

i have strange problem code.

webservice.coffe (simplificated)

servicemanager.service "webservice", class     constructor : (@$http) ->      # private      handleresult = (callback, errorcallback, autherrorcallback) =>         (result) =>             if result.result callback result             else                 if result.code 102 @logout autherrorcallback, errorcallback                 else errorcallback result.code, result.error      : (url, callback, errorcallback) ->         @$http.get url         .success handleresult callback, errorcallback, (=> @get url, callback, errorcallback)         .error handleerror errorcallback      logout : (callback, errorcallback) ->         @$http.get "logout"         .success callback()         .error errorcallback() 

in simplificated code, error _class.logout not function when handleresult called , error code 102.

the => operator should resolve problem it's not. don't understand why...


javascript compiled code of webservice.coffee

servicemanager.service("webservice", (function() {   var handleresult;    function _class($http) {     this.$http = $http;   }    handleresult = function(callback, errorcallback, autherrorcallback) {     return function(result) {       if (result.result) {         return callback(result);       } else {         if (result.code === 102) {           return _class.logout(autherrorcallback, errorcallback);         } else {           return errorcallback(result.code, result.error);         }       }     };   };    _class.prototype.get = function(url, callback, errorcallback) {     return this.$http.get(url).success(handleresult(callback, errorcallback, ((function(_this) {       return function() {         return _this.get(url, callback, errorcallback);       };     })(this)))).error(handleerror(errorcallback));   };    _class.prototype.logout = function(callback, errorcallback) {     return this.$http.get("logout").success(callback()).error(errorcallback());   };    return _class;  })()); 

your problem you're defining handleresult static, private variable inside class iife. cannot access @logout instance method therefore, cs compile static _class.logout invocation cannot work.

there few ways around this:

  • make handleresult private instance variable, , move inside constructor - declarations of @get , @logout well.
  • give handleresult instance parameter pass @, can invoke instance method on it
  • only call autherrorcallback , @logout , things inside there.

given service class instantiated once singleton anyway, first sensible.

however, should consider not doing complicated callback stuff anyway. use promises:

servicemanager.service "webservice", class     constructor : (@$http) ->      get: (url) ->         @$http.get url         .then (result) =>             if result.result result             else if result.code 102 @logout().then(=> @get url)             else throw result.error      logout: () ->         @$http.get "logout" 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -