Skip to content

Instantly share code, notes, and snippets.

@jasonkarns
Created December 3, 2013 21:16
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 jasonkarns/7777633 to your computer and use it in GitHub Desktop.
Save jasonkarns/7777633 to your computer and use it in GitHub Desktop.
Inverse _.partial
# we have an event aggregator that is treated as an adapter to hide
# whatever eventing lib/framework/utility we decide to use
#
# originally, it was backed by Backbone.Events. Now it's Angular's $rootScope
#
# `on` accepts a callback that expects to be given event args. (as backbone.events does)
#
# Angular's $on, on the other hand, accepts a callback that expects
# $scope as the first param, followed by event args.
# Below, our implementation just works around it by essentially wrapping the
# callback in another function that separates `scope` from the rest of the event args.
#
# This is done by splatting args[1]..args[n], and then just apply-ing them to the original handler
#
# This could probably be wrapped up as a utility function implemented using _.wrap
class Dispatcher
on: (eventName, handler, context) ->
@scope.$on eventName, (scope, args...) ->
handler.apply context, args
#...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment