Skip to content

Instantly share code, notes, and snippets.

@agincourt
Created July 9, 2009 12:08
Show Gist options
  • Select an option

  • Save agincourt/143602 to your computer and use it in GitHub Desktop.

Select an option

Save agincourt/143602 to your computer and use it in GitHub Desktop.
Prototype JS implementation of the data-method HTML5 attribute.
Event.observe(window, 'dom:loaded', function() {
setup_data_methods()
})
var setup_data_methods = function() {
$$('a[data-method]').each(function(el) {
el.stopObserving('click')
el.observe('click', run_data_method)
})
$$('form[data-method]').each(function(el) {
el.stopObserving('submit')
el.observe('submit', run_data_method)
})
}
var run_data_method = function(event) {
el = event.element()
data_method = el.readAttribute('data-method')
if(data_method.match(/\(/)) {
data_method.replace(/\)$/, ', event)')
} else {
data_method = data_method + '(event)'
}
eval(data_method)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment