Javascript 咖啡脚本碎片和棱角';s注射
在这种情况下:Javascript 咖啡脚本碎片和棱角';s注射,javascript,angularjs,coffeescript,browserify,Javascript,Angularjs,Coffeescript,Browserify,在这种情况下: # services.coffee module.exports = apiService : require './api-service' # api-service.coffee module.exports = ($http, $q, $window)-> (config)-> new ApiResource(config, $http, $q, $window) class ApiResource construc
# services.coffee
module.exports = apiService : require './api-service'
# api-service.coffee
module.exports = ($http, $q, $window)->
(config)->
new ApiResource(config, $http, $q, $window)
class ApiResource
constructor: (@config, @$http, @$q, @$window)->
....
# app.coffee
app = angular.module 'app',[]
app.factory(require('./services'))
我怎样才能很好地使用注射剂?我的意思是,如果我不使用类,而只是在一个函数中使用它(vars将在相同的范围内,等等),那么这将是可行的
不过我想使用一个类。那么,使用coffeescript的精彩功能(如splats、Mixin等)和angular的最佳方式是什么呢
这听起来很傻,但是,可能,也许
module.exports = ($http, $q, $window)->
(config)-> new ApiResource(config) # here I need somehow to pass those args
class ApiResource
constructor: (args...)->
$http = args['$http'] # or something like that...
除非我误解了你想要什么,否则它可能看起来像这样,使用对象模式匹配:
class ApiResource
constructor: ({@$http, @$q, @$window})->
@$http ?= window.$http # set a default
config =
$http: {foo: 'bar'}
api = new ApiResource(config)
alert api.$http.foo #=> 'bar'
这能让你更接近你想要的吗?像这样的东西怎么样
module.exports = (args...) ->
new ApiResource(args...)
class ApiResource
constructor: (args...) ->
[@$http, @$resource] = args
什么是module.exports?这是浏览器代码。@Freshyeball请想象我在使用Browserify,thanksOk,你的提供者声明在哪里?@Freshyeball我已经更新了我的问题,老实说,我不明白。你能详细说明一下吗?为什么要在curlies中传递它们?“curlies”对传入对象进行解构,并使它们作为
this立即可用。
查看api.$http
调用在代码示例中如何工作?它将config
分解为局部变量。这有意义吗?它似乎不起作用。。。也许我做错了什么是的。。。我的意思是不,不幸的是,angularjs注入器无法理解args…
part。