Skip to content

Instantly share code, notes, and snippets.

@Ibmurai Ibmurai/lol.js
Created Feb 6, 2012

Embed
What would you like to do?
A basic pattern for jQuery plugins
(function($) {
$.fn.lol = function() {
var scope = function($this) {
var f = {
/**
* The "constructor"
*/
init : function() {
console.log('init');
// Hook events up to functions in the f scope.
$this.click(f.events.click);
$this.bind('mycustomevent', f.events.customEvent);
},
/**
* Sample function - Sets the background color of $this.
*
* @param {String} color The color to set.
*/
setBg : function(color) {
console.log('setBg(' + color + ')');
$this.css('background-color', color);
},
events : {
/**
* Register the click, changing the background color.
*/
click : function() {
console.log('click');
f.setBg('green');
},
/**
* Log some custom event.
*/
customEvent : function() {
console.log('custom event!');
}
}
}
f.init();
}
return this.each(function() {
scope($(this));
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.