I use Coffeesctript as scripting- and testing-language for my angular 
application.
Coffescript is capable of defining classes, which I want to use in my 
application.
I want to wrap JSON opjects, as received from the server, into utility 
classes.

Let's say I have a page called 'dashboard', which displays objects, which 
are called boxes. So I have a the following controller:

class DashboardCtrl
  constructor: ->
    @ctrlName = 'DashboardCtrl'
    @boxes = [
      new Box { id: '12344555', name: 'Box 1', rootId: 'box1_rootId' },
      new Box { id: '12344555', name: 'Box 2', rootId: 'box2_rootId' },
      new Box { id: '12344555', name: 'Box 3', rootId: 'box3_rootId' },
      new Box { id: '12344555', name: 'Box 4', rootId: 'box3_rootId' },
      new Box { id: '12344555', name: 'Box 5', rootId: 'box3_rootId' }
    ]

angular
  .module('dashboard')
  .controller 'DashboardCtrl', ['BoxAPI', DashboardCtrl]

which should use the utility-class 'Box':

class Box
  constructor: (info) ->
    {@id, @name, @rootId} = info

This is tested, by the following unit test:

'use strict'

describe 'DashboardCtrl', ->
  ctrl = undefined

  beforeEach module 'dashboard', 'idgApis', 'lib'

  beforeEach inject ($rootScope, $controller) ->
    ctrl = $controller 'DashboardCtrl'

  it 'should load some boxes from the server', ->
    someBox = new Box {id: '12344555', name: 'Box 1', rootId: 'box1_rootId'}
    expect(ctrl.boxes).toContain someBox


which results in this output:

  DashboardCtrl > should load some boxes from the server
        ReferenceError: Can't find variable: Box
            at 
/home/roman/workspace/idg-frontend/build/app/js/dashboard/dashboard-controller.js:9
            at DashboardCtrl 
(/home/roman/workspace/idg-frontend/build/app/js/dashboard/dashboard-controller.js:9)
            at invoke 
(/home/roman/workspace/idg-frontend/bower_components/angular/angular.js:4185)
            at instantiate 
(/home/roman/workspace/idg-frontend/bower_components/angular/angular.js:4193)
            at 
/home/roman/workspace/idg-frontend/bower_components/angular/angular.js:8462
            at 
/home/roman/workspace/idg-frontend/build/test/app/dashboard/dashboard-controller_test.js:14
            at invoke 
(/home/roman/workspace/idg-frontend/bower_components/angular/angular.js:4185)
            at workFn 
(/home/roman/workspace/idg-frontend/bower_components/angular-mocks/angular-mocks.js:2364)
            at 
/home/roman/workspace/idg-frontend/node_modules/karma-jasmine/lib/boot.js:126
            at 
/home/roman/workspace/idg-frontend/node_modules/karma-jasmine/lib/adapter.js:171
            at http://localhost:9876/karma.js:185
            at http://localhost:9876/context.html:163
        undefined
        TypeError: 'undefined' is not an object (evaluating 'ctrl.boxes')
            at 
/home/roman/workspace/idg-frontend/build/test/app/dashboard/dashboard-controller_test.js:23
            at 
/home/roman/workspace/idg-frontend/node_modules/karma-jasmine/lib/boot.js:126
            at 
/home/roman/workspace/idg-frontend/node_modules/karma-jasmine/lib/adapter.js:171
            at http://localhost:9876/karma.js:185
            at http://localhost:9876/context.html:163

Is this possible in Angular? How do I make the utilities available? How to 
make it work?

Best regards

R.

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to