Skip to content

Instantly share code, notes, and snippets.

@printercu
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save printercu/658c58dee95758e97810 to your computer and use it in GitHub Desktop.
Save printercu/658c58dee95758e97810 to your computer and use it in GitHub Desktop.
plugin template
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