Skip to content

Instantly share code, notes, and snippets.

@dw
Created June 28, 2014 15:05
Show Gist options
  • Save dw/40d77f8689bdb0286c6b to your computer and use it in GitHub Desktop.
Save dw/40d77f8689bdb0286c6b to your computer and use it in GitHub Desktop.
/**
* 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