Skip to content

Instantly share code, notes, and snippets.

@jbarnette
Created January 17, 2012 18:54
Show Gist options
  • Save jbarnette/1628124 to your computer and use it in GitHub Desktop.
Save jbarnette/1628124 to your computer and use it in GitHub Desktop.
decorate: (n, fn) ->
target = this[n]
unless _.isFunction target
throw new Error "Can't decorate #{n}, it's not a Function."
unless target.fns?
wrap = ->
args = Array.prototype.slice.call arguments
finish = _.after wrap.fns.length, => wrap.old.apply this, args
_.each wrap.fns, (fn) => fn.call this, finish
_.extend wrap, fns: [], old: target
this[n] = target = wrap
target.fns.push fn
this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment