Created
July 9, 2012 16:47
-
-
Save erichmenge/3077541 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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