Skip to content

Instantly share code, notes, and snippets.

@Ibmurai
Created February 6, 2012 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ibmurai/1751125 to your computer and use it in GitHub Desktop.
Save Ibmurai/1751125 to your computer and use it in GitHub Desktop.
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