Skip to content

Instantly share code, notes, and snippets.

@drakmail
Last active January 2, 2016 14:19
Show Gist options
  • Select an option

  • Save drakmail/8315505 to your computer and use it in GitHub Desktop.

Select an option

Save drakmail/8315505 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) ->
if 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).trigger 'pageChanged'
$(document).on(
'page:done'
(event, $target, status, url, data) ->
$(document).find('[data-handler]').each ->
handlers.destroy @handlers
delete @handlers
$(document).trigger 'pageChanged'
)
handlers.register 'Overlay', class
constructor: (el) ->
@overlay = $ el
$(document).on 'overlay:hide', =>
@hide()
$(document).on 'overlay:show', =>
@show()
@overlay.on 'click', =>
$(document).trigger 'overlay:hide'
hide: =>
$(document).trigger('overlay:hidden')
@overlay.hide()
@overlay.removeClass('active')
show: =>
$(document).trigger('overlay:showed')
@overlay.show()
@overlay.addClass('active')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment