如何在AngularJs中使用Coffeescript实用程序类
我使用Coffeescript作为我的angular应用程序的脚本和测试语言。 CoffeeScript能够定义我想在应用程序中使用的类。 我想将从服务器接收到的JSON对象包装到实用程序类中 假设我有一个名为“dashboard”的页面,其中显示的对象称为框。因此,我有以下控制器:如何在AngularJs中使用Coffeescript实用程序类,angularjs,coffeescript,Angularjs,Coffeescript,我使用Coffeescript作为我的angular应用程序的脚本和测试语言。 CoffeeScript能够定义我想在应用程序中使用的类。 我想将从服务器接收到的JSON对象包装到实用程序类中 假设我有一个名为“dashboard”的页面,其中显示的对象称为框。因此,我有以下控制器: class DashboardCtrl constructor: -> @ctrlName = 'DashboardCtrl' @boxes = [ new Box { id:
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]
哪个应该使用实用程序类“Box”:
class Box
constructor: (info) ->
{@id, @name, @rootId} = info
通过以下单元测试对其进行测试:
'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
这将导致此输出:
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
这有可能吗?如何使公用设施可用?如何使其工作?首先在您的模块中声明,例如作为工厂:
angular
.module 'dashboard'
.factory 'Box', ->
class Box
constructor: (info) ->
{@id, @name, @rootId} = info
然后在测试中,您可以将其注入:
describe 'DashboardCtrl', ->
Box = undefined
beforeEach module 'dashboard'
beforeEach inject ($injector) ->
Box = $injector.get 'Box'
现在,您可以在it块中访问它。在哪里需要Box类的脚本?我不需要该类-问题是如何执行此操作。这会导致此错误:DashboardCtrl>应该从服务器加载一些框TypeError:“undefined”不是在Box中计算“info.id”的对象/home/roman/workspace/idg frontend/build/app/js/lib/box factory.js:9。。。