forked from jquery-boilerplate/jquery-boilerplate
/
jquery.boilerplate.coffee
45 lines (38 loc) · 1.52 KB
/
jquery.boilerplate.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Project:
# Description:
# Author:
# License:
# Note that when compiling with coffeescript, the plugin is wrapped in another
# anonymous function. We do not need to pass in undefined as well, since
# coffeescript uses (void 0) instead.
(($, window) ->
# window is passed through as local variable rather than global
# as this (slightly) quickens the resolution process and can be more efficiently
# minified (especially when both are regularly referenced in your plugin).
# Create the defaults once
pluginName = 'defaultPluginName'
document = window.document
defaults =
property: 'value'
# The actual plugin constructor
class Plugin
constructor: (@element, options) ->
# jQuery has an extend method which merges the contents of two or
# more objects, storing the result in the first object. The first object
# is generally empty as we don't want to alter the default options for
# future instances of the plugin
@options = $.extend {}, defaults, options
@_defaults = defaults
@_name = pluginName
@init()
init: ->
# Place initialization logic here
# You already have access to the DOM element and the options via the instance,
# e.g., @element and @options
# A really lightweight plugin wrapper around the constructor,
# preventing against multiple instantiations
$.fn[pluginName] = (options) ->
@each ->
if !$.data(this, "plugin_#{pluginName}")
$.data(@, "plugin_#{pluginName}", new Plugin(@, options))
)(jQuery, window)