Skip to content

Instantly share code, notes, and snippets.

@bunopus
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bunopus/0dfa04ce73b2b88c67b7 to your computer and use it in GitHub Desktop.
Save bunopus/0dfa04ce73b2b88c67b7 to your computer and use it in GitHub Desktop.
Angular "watcher" singletone in CoffeeScript
app.controller 'AppCtrl', ['DataLoggingWatcher', (DataLoggingWatcher) ->
#...
]
class DataLoggingWatcher
constructor: (@$rootScope) ->
@$rootScope.$on 'newData', () => @logToConsole()
logToConsole: () =>
console.log 'new data arrived'
angular.module('myApp')
.factory('DataLoggingWatcher', ['$rootScope', ($rootScope) ->
new DataLoggingWatcher($rootScope)
])
describe 'myApp.DataLoggingWatcher', ->
loggingWatcher = {}
rootScope = {}
beforeEach inject( (_$rootScope_)->
rootScope = _$rootScope_
)
beforeEach ->
loggingWatcher = new DataLoggingWatcher(rootScope)
describe 'constructor', ->
it 'should log on "newData" event', ->
spyOn(loggingWatcher,'logToConsole')
rootScope.$broadcast 'newData'
expect(loggingWatcher.logToConsole).toHaveBeenCalled()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment