Last active
August 29, 2015 14:07
-
-
Save bunopus/0dfa04ce73b2b88c67b7 to your computer and use it in GitHub Desktop.
Angular "watcher" singletone in CoffeeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.controller 'AppCtrl', ['DataLoggingWatcher', (DataLoggingWatcher) -> | |
#... | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DataLoggingWatcher | |
constructor: (@$rootScope) -> | |
@$rootScope.$on 'newData', () => @logToConsole() | |
logToConsole: () => | |
console.log 'new data arrived' | |
angular.module('myApp') | |
.factory('DataLoggingWatcher', ['$rootScope', ($rootScope) -> | |
new DataLoggingWatcher($rootScope) | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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