javascript - Coffescript compile constructor with wrong argument name -
i'm trying use first time coffeescript angularjs.
i want define new service dependence service $http
here i'm expecting:
var myservice = function($http) { this.$http = $http; }; myservice.prototype.call = function(url, data) { this.$http(url, data); }; myapp.service("webservice", myservice)
this normal way register service shown in angularjs documentation.
after reading article working coffeescript , angularjs, have tried this:
myapp.service "webservice", class constructor : (@$http) -> call : (url, data) -> @$http url, data
but result of compilation give javascript:
myapp.service("webservice", (function() { function _class(_at_$http) { this.$http = _at_$http; } _class.prototype.call = function(url, data) { return this.$http(url, data); }; return _class; })());
the problem coffeescript compiler shouldn't replace @$http
_at_$http
. in case should output javascript:
myapp.service("webservice", (function() { function _class($http) { this.$http = $http; } _class.prototype.call = function(url, data) { return this.$http(url, data); }; return _class; })());
you can view online compiler on coffeescript website give expected result, don't understand why mine not working well.
and need because of angular injection engine not recognize _at_$http
because expect $http
parameter name.
you need upgrade coffeescript 1.9.1 or higher:
- internal compiler variable names no longer start underscores. makes generated javascript bit prettier, , fixes issue broken , ungodly way angularjs "parses" function arguments.
if can't upgrade hook instance variable hand:
constructor : ($http) -> @$http = $http
everything works fine @ coffeescript.org because they're running latest version.
Comments
Post a Comment