Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions chapters/jquery/plugin.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
layout: recipe
title: Create a jQuery plugin
chapter: jQuery
---

h2. Problem

You'd like to create jQuery plugin using CoffeeScript

h2. Solution

{% highlight coffeescript %}
# Reference jQuery
$ = jQuery

# Adds plugin object to jQuery
$.fn.extend
# Change pluginName to your plugin's name.
pluginName: (options) ->
# Default settings
settings =
option1: true
option2: false
debug: false

# Merge default settings with options.
settings = $.extend settings, options

# Simple logger.
log = (msg) ->
console?.log msg if settings.debug

# _Insert magic here._
return @each ()->
log "Preparing magic show."
# You can use your settings in here now.
log "Option 1 value: #{settings.option1}"
{% endhighlight %}

h2. Discussion

h3. Usage

Here are a couple of examples of how to use your new plugin.

h4. JavaScript

{% highlight javascript %}
$("body").pluginName({
debug: true
};

{% endhighlight %}

h4. CoffeeScript:

{% highlight coffeescript %}
$("body").pluginName
debug: true

{% endhighlight %}