Created
July 9, 2009 12:08
-
-
Save agincourt/143602 to your computer and use it in GitHub Desktop.
Prototype JS implementation of the data-method HTML5 attribute.
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
| 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