From 2464b333c307d5029e18ce816629fbe27e429736 Mon Sep 17 00:00:00 2001 From: Jesse House Date: Sat, 28 May 2011 15:38:34 -0700 Subject: [PATCH 1/2] new recipe: create object literal if it does not exist --- ...create-object-literal-if-not-exist.textile | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 chapters/objects/create-object-literal-if-not-exist.textile diff --git a/chapters/objects/create-object-literal-if-not-exist.textile b/chapters/objects/create-object-literal-if-not-exist.textile new file mode 100644 index 0000000..e642240 --- /dev/null +++ b/chapters/objects/create-object-literal-if-not-exist.textile @@ -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. From f2d2d7c2844ef9a439f50f6291ea18957f55aae8 Mon Sep 17 00:00:00 2001 From: Jesse House Date: Sat, 28 May 2011 15:40:04 -0700 Subject: [PATCH 2/2] new recipe: jquery ajax --- chapters/jquery/ajax.textile | 33 +++++++++++++++++++++++++++++++++ wanted-recipes.textile | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 chapters/jquery/ajax.textile diff --git a/chapters/jquery/ajax.textile b/chapters/jquery/ajax.textile new file mode 100644 index 0000000..4fb319d --- /dev/null +++ b/chapters/jquery/ajax.textile @@ -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. \ No newline at end of file diff --git a/wanted-recipes.textile b/wanted-recipes.textile index a0c37de..61c8e81 100644 --- a/wanted-recipes.textile +++ b/wanted-recipes.textile @@ -108,7 +108,7 @@ console.log [1,2,3,4,5].map (x) -> h2. jQuery -* jQuery AJAX from CS +* h2. Regular Expressions