Created
July 1, 2013 20:40
-
-
Save alininja/5904421 to your computer and use it in GitHub Desktop.
Bootstrap-ish jQuery plugin structure in CoffeeScript
This file contains hidden or 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
((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