Skip to content

Instantly share code, notes, and snippets.

@Bolza
Last active December 23, 2015 14:45
Show Gist options
  • Save Bolza/4e75c9890a07973ad4c8 to your computer and use it in GitHub Desktop.
Save Bolza/4e75c9890a07973ad4c8 to your computer and use it in GitHub Desktop.
//`first` reads the data from the service
Controller first (myService) ->
this.firstValue = myService.get().firstValue //using a getter so there's no direct reference
myService.onChange(function(newData) {
this.firstValue = newData.firstValue
})
//`second` sets the data inside the service
Controller second (myService) ->
myService.set('firstValue', this.secondValue);
// 'myService' can handle the event subscription
Service myService ($http, $rootScope) ->
function get ->
$http.get().then(fireEvent)
// using the $rootScope (if we wont have many of these istances)
function onChange(cb) ->
$rootScope.$on('myService:change', cb)
function fireEvent (data)->
$rootScope.$emit('myService:change', data)
// or we can use a custom pub/sub class that doesnt touch the rootScope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment