bqfulg
Last Updated: February 25, 2016
·
719
· glebm

jQuery prependEvent in 5 lines of coffeescript

Works on jQuery 1.9.

$.fn.prebind = (name, handler) ->
  @on name, handler
  @each ->
    handlers = $._data(@, 'events')[name.split('.')[0]]
    # move to the beginning
    handlers.splice 0, 0, handlers.pop()

This uses internal API ($._data) and might break in the future versions of jQuery

Adapted from http://stackoverflow.com/questions/2360655/jquery-event-handlers-always-execute-in-order-they-were-bound-any-way-around-t