Skip to content

Instantly share code, notes, and snippets.

@markupboy
Last active December 11, 2015 05:29
Show Gist options
  • Save markupboy/4552507 to your computer and use it in GitHub Desktop.
Save markupboy/4552507 to your computer and use it in GitHub Desktop.
coffeescript version of thie highly configurable plugin pattern - http://markdalgleish.com/2011/05/creating-highly-configurable-jquery-plugins/
class Plugin
constructor: (elem, options) ->
@elem = elem
@$elem = $(elem)
@options = options
@metadata = @$elem.data('plugin-options')
@
defaults:
message: 'Hello world!'
init: ->
@config = $.extend {}, @defaults, @options, @metadata
@displayMessage()
@
displayMessage: ->
alert @config.message
Plugin.defaults = Plugin.prototype.defaults;
$.fn.plugin = (options) ->
@each ->
(new Plugin @, options).init()
window.Plugin = Plugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment