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/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. 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