Skip to content

Commit 4618c34

Browse files
committed
Merge pull request coffeescript-cookbook#16 from spadin/master
Adding a jQuery plugin template.
2 parents a7787f4 + d8d68a5 commit 4618c34

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

chapters/jquery/plugin.textile

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
layout: recipe
3+
title: Create a jQuery plugin
4+
chapter: jQuery
5+
---
6+
7+
h2. Problem
8+
9+
You'd like to create jQuery plugin using CoffeeScript
10+
11+
h2. Solution
12+
13+
{% highlight coffeescript %}
14+
# Reference jQuery
15+
$ = jQuery
16+
17+
# Adds plugin object to jQuery
18+
$.fn.extend
19+
# Change pluginName to your plugin's name.
20+
pluginName: (options) ->
21+
# Default settings
22+
settings =
23+
option1: true
24+
option2: false
25+
debug: false
26+
27+
# Merge default settings with options.
28+
settings = $.extend settings, options
29+
30+
# Simple logger.
31+
log = (msg) ->
32+
console?.log msg if settings.debug
33+
34+
# _Insert magic here._
35+
return @each ()->
36+
log "Preparing magic show."
37+
# You can use your settings in here now.
38+
log "Option 1 value: #{settings.option1}"
39+
{% endhighlight %}
40+
41+
h2. Discussion
42+
43+
h3. Usage
44+
45+
Here are a couple of examples of how to use your new plugin.
46+
47+
h4. JavaScript
48+
49+
{% highlight javascript %}
50+
$("body").pluginName({
51+
debug: true
52+
};
53+
54+
{% endhighlight %}
55+
56+
h4. CoffeeScript:
57+
58+
{% highlight coffeescript %}
59+
$("body").pluginName
60+
debug: true
61+
62+
{% endhighlight %}

0 commit comments

Comments
 (0)