Skip to content

Instantly share code, notes, and snippets.

@jadehopepunk
Last active December 20, 2015 23:58
Show Gist options
  • Save jadehopepunk/6215971 to your computer and use it in GitHub Desktop.
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
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