CoffeeScript for Angular Services

61 views
Skip to first unread message

Bob Weiner Jr

unread,
Oct 24, 2013, 12:01:29 PM10/24/13
to ang...@googlegroups.com
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.


 

 

OpenNota

unread,
Oct 24, 2013, 6:29:55 PM10/24/13
to ang...@googlegroups.com
app.factory 'DevService', ['$http', '$q', ($http, $q) -> new DevService($http, $q)]

Reply all
Reply to author
Forward
0 new messages