Skip to content

Commit a2c0020

Browse files
committed
Merge upstream/master.
2 parents a96c159 + 27d409b commit a2c0020

File tree

7 files changed

+96
-43
lines changed

7 files changed

+96
-43
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
layout: recipe
3+
title: Class Variables
4+
chapter: Classes and Objects
5+
---
6+
7+
h2. Creating Class Variables
8+
9+
You want to create a class variable.
10+
11+
h2. Solution
12+
13+
Use json notation in the class body; use :: to access it outside:
14+
15+
{% highlight coffeescript %}
16+
class Zoo
17+
MAX_ANIMALS: 50
18+
19+
Zoo::MAX_ZOOKEEPERS = 5
20+
21+
Zoo::MAX_ANIMALS
22+
# => 50
23+
{% endhighlight %}
24+
25+
h2. Discussion
26+
27+
Coffeescript will store these values on the class prototype (e.g. Zoo.prototype.MAX_ANIMALS) rather than on individual object instances, which conserves memory and gives a central location to store class-level values.

chapters/objects/cloning.textile renamed to chapters/classes_and_objects/cloning.textile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: recipe
33
title: Cloning an object (deep copy)
4-
chapter: Objects
4+
chapter: Classes and Objects
55
---
66

77
h2. Problem
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
layout: recipe
3+
title: Create an object literal if it does not already exist
4+
chapter: Classes and Objects
5+
---
6+
7+
h2. Problem
8+
9+
You want to initialize an object literal, but you do not want to overwrite the object if it already exists.
10+
11+
12+
h2. Solution
13+
14+
Use the Existential operator
15+
16+
{% highlight coffeescript %}
17+
window.MY_NAMESPACE ?= {}
18+
{% endhighlight %}
19+
20+
21+
h2. Discussion
22+
23+
This is equivalent to the following JavaScript:
24+
25+
{% highlight javascript %}
26+
window.MY_NAMESPACE = window.MY_NAMESPACE || {};
27+
{% endhighlight %}
28+
29+
Common JavaScript technique, using object literal to define a namespace. This saves us from clobbering the namespace if it already exists.

chapters/objects/index.textile renamed to chapters/classes_and_objects/index.textile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: chapter
3-
title: Objects
4-
chapter: Objects
3+
title: Classes and Objects
4+
chapter: Classes and Objects
55
---
66

77
{% capture url %}/chapters/{{ page.chapter | replace: ' ', '_' | downcase }}{% endcapture %}

chapters/index.textile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: default
33
title: Cookbook
44
chapters:
55
- Syntax
6-
- Objects
6+
- Classes and Objects
77
- Strings
88
- Arrays
99
- Dates and Times

chapters/jquery/ajax.textile

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,50 @@ chapter: jQuery
66

77
h2. Problem
88

9-
You want to make AJAX calls using jQuery.
9+
You need to make an AJAX request using jQuery.
1010

1111
h2. Solution
1212

1313
{% highlight coffeescript %}
14-
$ ?= require 'jquery' # For Node.js compatibility
14+
$ ?= require 'jquery' # Optional. For Node.js compatibility
15+
16+
jQuery ->
17+
console.log "document loaded"
18+
$('form').submit (e) ->
19+
console.log "form submit"
20+
e.preventDefault()
21+
form = this
22+
23+
$.ajax
24+
type: "POST"
25+
url: $(form).attr('action')
26+
data: $(form).serialize()
27+
success: ->
28+
console.log("success")
1529

16-
$(document).ready ->
17-
# Basic Examples
30+
{% endhighlight %}
31+
32+
h2. Discussion
33+
34+
In addition to the Swiss Army knife of jQuery AJAX methods - _ajax()_ - the library also provides convenience methods for specific kinds of requests. Note that the _jQuery_ and _$_ variables can be used interchangeably.
35+
36+
{% highlight coffeescript %}
37+
$ ->
1838
$.get '/', (data) ->
1939
$('body').append "Successfully got the page."
2040

2141
$.post '/',
2242
userName: 'John Doe'
2343
favoriteFlavor: 'Mint'
2444
(data) -> $('body').append "Successfully posted to the page."
45+
{% endhighlight %}
46+
47+
jQuery 1.5 and later have added a new, supplimental API for handling different callbacks.
2548

26-
# Advanced Settings
27-
$.ajax '/',
28-
type: 'GET'
29-
dataType: 'html' error: (jqXHR, textStatus, errorThrown) ->
30-
$('body').append "AJAX Error: #{textStatus}"
31-
success: (data, textStatus, jqXHR) ->
32-
$('body').append "Successful AJAX call: #{data}"
33-
34-
# For jQuery 1.5+
49+
{% highlight coffeescript %}
3550
request = $.get '/'
3651
request.success (data) -> $('body').append "Successfully got the page again."
3752
request.error (jqXHR, textStatus, errorThrown) -> $('body').append "AJAX Error: ${textStatus}."
38-
3953
{% endhighlight %}
4054

41-
h2. Discussion
42-
43-
See the "jQuery AJAX API":http://api.jquery.com/jQuery.ajax/ for additional methods and options.
55+
See also "Callback bindings":../jquery/callback-bindings-jquery and the "jQuery AJAX API":http://api.jquery.com/jQuery.ajax/ for additional methods and options.

chapters/math/generating-predictable-random-numbers.textile

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,21 @@ class Rand
2828
@seed = seed
2929

3030
# return a random integer 0 <= n < @modulo
31-
randn:
31+
randn: ->
3232
# new_seed = (a * seed + c) % m
3333
@seed = (@multiplier*@seed + @offset) % @modulo
3434

3535
# return a random float 0 <= f < 1.0
36-
rand: ->
36+
randf: ->
3737
this.randn() / @modulo
3838

3939
# return a random int 0 <= f < n
40-
randi: (n) ->
41-
Math.floor(this.rand() * n)
40+
rand: (n) ->
41+
Math.floor(this.randf() * n)
4242

43-
r = new Rand 0
44-
r.randn().toString(16)
45-
# => "3c6ef35f"
46-
47-
r.randn().toString(16)
48-
# => "47502932"
49-
50-
r.randn().toString(16)
51-
# => "d1ccf6e9"
52-
53-
r.randn().toString(16)
54-
# => "aaf95334"
55-
56-
r.randn().toString(16)
57-
# => "6252e503"
58-
59-
r.randn().toString(16)
60-
# => "9f2ec686"
43+
# return a random int min <= f < max
44+
rand2: (min, max) ->
45+
min + this.rand(max-min)
6146
{% endhighlight %}
6247

6348
h2. Discussion

0 commit comments

Comments
 (0)