visoft
10/10/2014 - 12:35 PM

Simple jQuery Plugin with CoffeeScript

Simple jQuery Plugin with CoffeeScript

$ = jQuery

$.myPlugin = (element) ->
  plugin = this
  
  plugin.init = ->
    # Do stuff here
  
  # initialize the plugin
  plugin.init()

  # return the plugin, otherwise we can't chain commands
  plugin
  

$.fn.myPlugin = ->
  @each ->
    if undefined is $(this).data('myPlugin')
      plugin = new $.myPlugin(this)
      $(this).data 'myPlugin', plugin