Created
February 6, 2012 09:50
-
-
Save Ibmurai/1751125 to your computer and use it in GitHub Desktop.
A basic pattern for jQuery plugins
This file contains 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
(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