27 Feb 2013

jQuery custom plugin skeleton with CoffeeScript

This snippet might be used as a skeleton for simple jQuery plugins to be used inside your web apps:

# jQuery custom plugin skeleton with CoffeeScript
class MyPlugin
  someMethod: (args) ->
    # do something

  constructor: (element, params) ->
    @someMethod(some_args)
    # do something

# install in the window namespace
window.MyPlugin = MyPlugin

# create jQuery plugin
$.fn.myPlugin = (params) ->
  new MyPlugin($(@), params)
  return $(@)

# Usage:
# $('#element').myPlugin(params)

NOTE: also check discussions on this gist.

Enjoyed this article? Share it!