Skip to content

Instantly share code, notes, and snippets.

@alininja
Created July 1, 2013 20:40
Show Gist options
  • Save alininja/5904421 to your computer and use it in GitHub Desktop.
Save alininja/5904421 to your computer and use it in GitHub Desktop.
Bootstrap-ish jQuery plugin structure in CoffeeScript
((context, $) ->
# COOL WIDGET CLASS DEFINITION
#===================================
context = (element, options) ->
@options = options
@$element = $(element).on('change', 'input[type=checkbox]', $.proxy(@update, this))
context:: =
constructor: context
update: ->
@$element # do something cool
# COOL WIDGET DEFINITION
#===================================
$.fn.coolWidget = (option) ->
@each ->
$this = $(this)
data = $this.data('coolWidget')
options = $.extend({}, $.fn.coolWidget.defaults, $this.data(), typeof option is 'object' and option)
$this.data 'coolWidget', (data = new context(this, options)) unless data
data[option]() if typeof option is 'string'
$.fn.coolWidget.defaults =
$target_element: true
) window.coolWidget = window.coolWidget or {}, jQuery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment