Skip to content

Instantly share code, notes, and snippets.

@erichmenge
Created July 9, 2012 16:47
Show Gist options
  • Save erichmenge/3077541 to your computer and use it in GitHub Desktop.
Save erichmenge/3077541 to your computer and use it in GitHub Desktop.
class Handlers
constructor: ->
@handlers = {}
register: (handler, registered_class) ->
@handlers[handler] = registered_class
instantiate: (handlers, element) ->
handlers = handlers.replace(/\s/g, '').split(',')
element.handlers = []
$.each handlers, (index, handler) =>
if @handlers[handler]
instance = new @handlers[handler](element)
element.handlers.push instance
else
throw "Unknown handler " + handler
destroy: (instances) ->
$.each instances, (index, instance) ->
instance.destroy() if instance.destroy
@handlers = new Handlers
$(document).on 'pageChanged', ->
$('[data-handler]').each ->
handlers.instantiate $(@).attr('data-handler'), @
$(document).on 'pageUpdated', ->
$('[data-pjax-container]').find('[data-handler]').each ->
handlers.instantiate $(@).attr('data-handler'), @
$(document).on 'pjax:start', ->
$(".flashes").html('')
$('[data-pjax-container]').find('[data-handler]').each ->
handlers.destroy @handlers
delete @handlers
$(document).on 'pjax:end', ->
$(document).trigger 'pageUpdated'
$ ->
$(document).trigger 'pageChanged'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment