Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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