Skip to content
Merged
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions chapters/jquery/ajax.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
layout: recipe
title: Ajax
chapter: jQuery
---

h2. Problem

You need to make an ajax request using jQuery.


h2. Solution

{% highlight coffeescript %}
jQuery ->
console.log "document loaded"
$('form').submit (e) ->
console.log "form submit"
e.preventDefault()
form = this

$.ajax
type: "POST"
url: $(form).attr('action')
data: $(form).serialize()
success: ->
console.log("success")
{% endhighlight %}


h2. Discussion

The jQuery and $ variables can be used interchangeably. See also "Callback bindings":../jquery/callback-bindings-jquery.
29 changes: 29 additions & 0 deletions chapters/objects/create-object-literal-if-not-exist.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
layout: recipe
title: Create an object literal if it does not already exist
chapter: Objects
---

h2. Problem

You want to initialize an object literal, but you do not want to overwrite the object if it already exists.


h2. Solution

Use the Existential operator

{% highlight coffeescript %}
window.MY_NAMESPACE ?= {}
{% endhighlight %}


h2. Discussion

This is equivalent to the following JavaScript:

{% highlight javascript %}
window.MY_NAMESPACE = window.MY_NAMESPACE || {};
{% endhighlight %}

Common JavaScript technique, using object literal to define a namespace. This saves us from clobbering the namespace if it already exists.
2 changes: 1 addition & 1 deletion wanted-recipes.textile
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ console.log [1,2,3,4,5].map (x) ->

h2. jQuery

* jQuery AJAX from CS
*

h2. Regular Expressions

Expand Down