I am refactoring a project and would like to use CoffeeScript classes to create my angular components. I have had some success but cannot figure out services. What I have so far is:
RoutesDev.coffee
class RoutesDev
constructor: (@$routeProvider) ->
@$routeProvider.when '/ReleaseNotes',
templateUrl: '/Dev/ReleaseNotes'
controller: 'ReleaseNotesCtrl'
app.config(RoutesDev)
ReleaseNotesCtrl.coffee
class ReleaseNotesCtrl
constructor: (@$scope, @$http, @DevService) ->
@getDataAsync()
$scope.ctrl = @
onDataLoad: (data) ->
@$scope.notes = data
onDataError: (config) ->
console.log config
getDataAsync: ->
@DevService.getReleaseNotes @onDataLoad, @onDataError # failed line
# This works without the service implementation
# @$http
# .get('/api/dev/')
# .success (data) =>
# @$scope.notes = data
app.controller 'ReleaseNotesCtrl',
['$scope', '$http', 'DevService', ReleaseNotesCtrl]
DevService.coffee
class DevService
constructor: (@$http, @$q) ->
getReleaseNotes: (cbSuccess, cbError) ->
@$http
.get('/api/dev/')
.then(cbSuccess, cbError)
app.factory 'DevService',
['$http', '$q', DevService]
I've tried several tweaks on the above but cannot get rid of the error:
TypeError: Cannot call method 'getReleaseNotes' of undefined
Registering coffee classes with app.config and app.controller works as expected but I am missing something with app.factory. The javascript version of the code works so the code logic should be ok.