Last active
August 29, 2015 14:05
-
-
Save printercu/658c58dee95758e97810 to your computer and use it in GitHub Desktop.
plugin template
This file contains 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
do ($ = jQuery) -> | |
plugin_name = 'pluginName' | |
data_index = "#{plugin_name}.instance" | |
# Example: | |
# | |
# $. | |
$[plugin_name] = class PluginClassName | |
@init: -> | |
$('[data-plugin-selector]')[plugin_name]() | |
@liveClick: (event) -> | |
$(@)[plugin_name] 'someAction' | |
constructor: (@element, @options = {}) -> | |
# ... | |
@bind() | |
bind: -> | |
@element.click (event) => | |
@action() | |
$.fn[plugin_name] = (action, options) -> | |
if typeof action is 'object' | |
options = action | |
action = null | |
@each -> | |
$this = $ @ | |
unless instance = $this.data data_index | |
instance = new $[plugin_name] $this, options | |
$this.data data_index, instance | |
instance[action](options) if action | |
# To init once. For plugins, that should start working on page load. | |
$ -> $[plugin_name].init() unless $[plugin_name].skipAutoinit | |
# $(window).on 'load', -> $[plugin_name].init() unless $[plugin_name].skipAutoinit | |
# and/or | |
# Lazy load on some action | |
$(document).on 'click', '[data-plugin-selector]', $[plugin_name].liveClick |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment