Skip to content

Instantly share code, notes, and snippets.

@stefanoverna
Created July 19, 2013 16:15
Show Gist options
  • Select an option

  • Save stefanoverna/6040405 to your computer and use it in GitHub Desktop.

Select an option

Save stefanoverna/6040405 to your computer and use it in GitHub Desktop.
class @Behaviour
constructor: (@dom, @options = {}) ->
@$dom = $(@dom)
className = @getClassName()
unless @$dom.data(className)
@$dom.data(className, true)
@init()
data: (attr, def=null) ->
@options[attr] || @$dom.data(attr) || def
domData: (attr) ->
$ @data(attr)
getClassName: ->
@constructor.className
init: ->
init = ->
$('[data-behaviour]').each ->
behaviours = $(@).data('behaviour').split(/\s+/)
for behaviour in behaviours
klass = window[behaviour]
if klass
klass.className = behaviour
new klass(@)
else
console.error "Class #{behaviour} not found"
$ -> init()
$(document).ajaxComplete -> init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment