Last active
December 20, 2015 23:58
-
-
Save jadehopepunk/6215971 to your computer and use it in GitHub Desktop.
A coffee script version of the jQuery plugin structure outlined at http://engineering.yp.com/post/jquery-oo-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
class MyPlugin | |
constructor: (element, options) -> | |
@elem = $(element) | |
@settings = $.extend(param: "defaultValue", options or {}) | |
# Public method | |
publicMethod: () -> | |
console.log "publicMethod() called!" | |
$.fn.myplugin = (options) -> | |
@each -> | |
element = $(this) | |
dataName = 'myplugin' | |
# Return early if this element already has a plugin instance | |
return if element.data(dataName) | |
# pass options to plugin constructor | |
myplugin = new MyPlugin(this, options) | |
# Store plugin object in this element's data | |
element.data dataName, myplugin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment