Created
June 28, 2014 15:05
-
-
Save dw/40d77f8689bdb0286c6b 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
/** | |
* Match methods on `obj` of the form "_onCssClassAction", extracting | |
* "CssClass" and "Action", then: | |
* | |
* - Transform "CssClass" to ".cssClass" | |
* - Transform "Action" to "action" | |
* - Find elements matching .cssClass inside `elem` | |
* - Wire up the corresponding jQuery action (e.g. click()) to a bound | |
* copy of the method. | |
* | |
* <button class="stripeBtn"></button> | |
* _onStripeBtnClick | |
* becomes: | |
* $('.stripBtn', elem).click(_onStripeBtnClick.bind(obj)); | |
*/ | |
xxx.wireUp = function(elem, obj) | |
{ | |
elem = $(elem); | |
for(var k in obj) { | |
var bits = k.match(/_on(.+)([A-Z][a-z]+)/) || []; | |
var methName = xxx.camelize(bits[2]); | |
if(methName && elem[methName]) { | |
var className = xxx.camelize(bits[1]); | |
$('.' + className, elem)[methName](obj[k].bind(obj)); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment