From d151a3e8b98326f4a0b9326633d7a02481916f5e Mon Sep 17 00:00:00 2001 From: Connor Montgomery Date: Sun, 19 Feb 2012 20:07:46 -0600 Subject: [PATCH 01/15] better serialization section on ajax-and-forms --- content/ajax/ajax-and-forms.md | 43 ++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/content/ajax/ajax-and-forms.md b/content/ajax/ajax-and-forms.md index 14e761a8..a01d80bc 100644 --- a/content/ajax/ajax-and-forms.md +++ b/content/ajax/ajax-and-forms.md @@ -1,27 +1,50 @@ +- Right now we're really not covering the fundamentals of how Ajax works with forms. Traditional form handling vs the new. $.ajax has the power to greatly change everything from validation (e.g 'sorry, your username is taken') through to prefiltering but we've giving a very very minor summary of what is possible. I think we need to somehow address this. A first attempt might at least bring over more examples from the docs. +- Locate articles that cover this well and see if we can borrow from them. As per the other sections, I do not think we want to end up in another situation (as with the old site) where we're linking to articles that end up outdated with time, so borrow or write this out ourselves. + +c: Addy, I'm planning on adjusting the ajax-and-forms section. One quick question, though - do we want to promote using the plugin, or teach how to go about doing the things we can with $.ajax sans-plugin? + +I think the best way to approach that section in particular is to explain what serialize and serializeArray do and why they're important/useful, and then go in to giving specific examples as to the things you mentioned above (validation, prefiltering, etc). + +a: @connormontgomery imo, we should approach it without using the plugin. I agree with serialize()/serializeArray() being explained first and then reviewing the other examples. + --- chapter : ajax section : 4 title : Ajax and Forms attribution: jQuery Fundamentals + --- -jQuery’s ajax capabilities can be especially useful when dealing with forms. -The [jQuery Form Plugin](http://jquery.malsup.com/form/) is a well-tested tool -for adding Ajax capabilities to forms, and you should generally use it for -handling forms with Ajax rather than trying to roll your own solution for -anything remotely complex. That said, there are a two jQuery methods you -should know that relate to form processing in jQuery: `$.fn.serialize` and -`$.fn.serializeArray`. + +jQuery’s ajax capabilities can be especially useful when dealing with forms. There are several advantages, which can range from simple client-side validation (e.g. "Sorry, that username is taken"), to serialization, to [prefilters](http://api.jquery.com/extending-ajax/#Prefilters) (explained below), and even more! + +### Client-side validation + + +### Serialization +Serializing form inputs in jQuery is extremely easy. Two methods come supported natively - `$.fn.serialize` and `$.fn.serializeArray`. While the names are fairly self-explanatory, there are many advantages to using them. + +The `serialize` method serializes a form's data into a query string. For the element's value to be serialized, it **must** have a `name` attribute. Please noted that values from inputs with a type of `checkbox` or `radio` are included only if they are checked. + -$('#myForm').serialize(); +$('#myForm').serialize(); // creates a query string like this: field_1=something&field2=somethingElse +While plain old serialization is great, sometimes your application would work better if you sent over an array of objects, instead of just the query string. For that, jQuery has the `serializeArray` method. It's very similar to the `serialize` method listed above, except it produces an array of objects, instead of a string. + $('#myForm').serializeArray(); // creates a structure like this: [ - { name : 'field1', value : 123 }, - { name : 'field2', value : 'hello world' } + { name : 'field_1', value : 'something' }, + { name : 'field_2', value : 'somethingElse' } ] + + +### Prefiltering + +anything remotely complex. That said, there are a two jQuery methods you +should know that relate to form processing in jQuery: `$.fn.serialize` and +`$.fn.serializeArray`. \ No newline at end of file From cd4acf6583f0d6215f021d3b38b433204cbc852e Mon Sep 17 00:00:00 2001 From: Connor Montgomery Date: Sun, 19 Feb 2012 21:25:13 -0600 Subject: [PATCH 02/15] prefiltering section of ajax/ajax-and-forms; started on the client-side validation section --- content/ajax/ajax-and-forms.md | 39 ++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/content/ajax/ajax-and-forms.md b/content/ajax/ajax-and-forms.md index a01d80bc..0af14da6 100644 --- a/content/ajax/ajax-and-forms.md +++ b/content/ajax/ajax-and-forms.md @@ -15,10 +15,7 @@ attribution: jQuery Fundamentals --- -jQuery’s ajax capabilities can be especially useful when dealing with forms. There are several advantages, which can range from simple client-side validation (e.g. "Sorry, that username is taken"), to serialization, to [prefilters](http://api.jquery.com/extending-ajax/#Prefilters) (explained below), and even more! - -### Client-side validation - +jQuery’s ajax capabilities can be especially useful when dealing with forms. There are several advantages, which can range from serialization, to simple client-side validation (e.g. "Sorry, that username is taken"), to [prefilters](http://api.jquery.com/extending-ajax/#Prefilters) (explained below), and even more! ### Serialization Serializing form inputs in jQuery is extremely easy. Two methods come supported natively - `$.fn.serialize` and `$.fn.serializeArray`. While the names are fairly self-explanatory, there are many advantages to using them. @@ -43,8 +40,36 @@ $('#myForm').serializeArray(); +### Client-side validation +Client-side validation is, much like many other things, extremely easy using jQuery. While there are several cases developers can test for, some of the most common ones are: presence of a required input, valid usernames/emails, or checking an "I agree..." box. + +Please note that it is advisable that you also perform server-side validation for your inputs. However, it typically makes for a better user experience to be able to validate some things without submitting the form. + +With that being said, let's jump on in to some examples: + + + + ### Prefiltering +A prefilter is a way to modify the ajax options before each request is sent (hence, the name `prefilter`). + +For example, say we would like to modify all crossDomain requests through a proxy. To do so with a prefilter is quite simple: + + +$.ajaxPrefilter(function( options, originalOptions, jqXHR ) { + if ( options.crossDomain ) { + options.url = "http://mydomain.net/proxy/" + encodeURIComponent(options.url ); + options.crossDomain = false; + } +}); + + +You can pass in an optional argument before the callback function that specifies which `dataTypes` you'd like the prefilter to be applied to. For example, if we want our prefilter to only apply to `JSON` and `script` requests, we'd do: + + +$.ajaxPrefilter( "json script", function( options, originalOptions, jqXHR ) { + // do all of the prefiltering here, but only for requests that indicate a dataType of "JSON" or "script" +}) + -anything remotely complex. That said, there are a two jQuery methods you -should know that relate to form processing in jQuery: `$.fn.serialize` and -`$.fn.serializeArray`. \ No newline at end of file +As you can see, prefiltering with jQuery is a simple, yet quite under-utilized, way to modify your requests to your liking. \ No newline at end of file From 7a71dbc4d016b1b9c421eb511024d2764ca5c5ad Mon Sep 17 00:00:00 2001 From: Connor Montgomery Date: Sun, 19 Feb 2012 21:47:44 -0600 Subject: [PATCH 03/15] add client-side validation section in ajax/ajax-with-forms --- content/ajax/ajax-and-forms.md | 54 ++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/content/ajax/ajax-and-forms.md b/content/ajax/ajax-and-forms.md index 0af14da6..dc78e052 100644 --- a/content/ajax/ajax-and-forms.md +++ b/content/ajax/ajax-and-forms.md @@ -1,12 +1,3 @@ -- Right now we're really not covering the fundamentals of how Ajax works with forms. Traditional form handling vs the new. $.ajax has the power to greatly change everything from validation (e.g 'sorry, your username is taken') through to prefiltering but we've giving a very very minor summary of what is possible. I think we need to somehow address this. A first attempt might at least bring over more examples from the docs. -- Locate articles that cover this well and see if we can borrow from them. As per the other sections, I do not think we want to end up in another situation (as with the old site) where we're linking to articles that end up outdated with time, so borrow or write this out ourselves. - -c: Addy, I'm planning on adjusting the ajax-and-forms section. One quick question, though - do we want to promote using the plugin, or teach how to go about doing the things we can with $.ajax sans-plugin? - -I think the best way to approach that section in particular is to explain what serialize and serializeArray do and why they're important/useful, and then go in to giving specific examples as to the things you mentioned above (validation, prefiltering, etc). - -a: @connormontgomery imo, we should approach it without using the plugin. I agree with serialize()/serializeArray() being explained first and then reviewing the other examples. - --- chapter : ajax section : 4 @@ -33,20 +24,47 @@ While plain old serialization is great, sometimes your application would work be $('#myForm').serializeArray(); // creates a structure like this: -[ - { name : 'field_1', value : 'something' }, - { name : 'field_2', value : 'somethingElse' } -] +// [ +// { name : 'field_1', value : 'something' }, +// { name : 'field_2', value : 'somethingElse' } +// ] - ### Client-side validation -Client-side validation is, much like many other things, extremely easy using jQuery. While there are several cases developers can test for, some of the most common ones are: presence of a required input, valid usernames/emails, or checking an "I agree..." box. +Client-side validation is, much like many other things, extremely easy using jQuery. While there are several cases developers can test for, some of the most common ones are: presence of a required input, valid usernames/emails/phone numbers/etc..., or checking an "I agree..." box. Please note that it is advisable that you also perform server-side validation for your inputs. However, it typically makes for a better user experience to be able to validate some things without submitting the form. -With that being said, let's jump on in to some examples: +With that being said, let's jump on in to some examples! First, we'll see how easy it is to check if a required field doesn't have anything in it. If it doesn't, then we'll `return false`, and prevent the form from processing. + +$("#form").submit(function( e ) { + if ( $(".required").val().length === 0 ) { // if .required's value's length is zero + // usually show some kind of error message here + return false; // this prevents the form from submitting + } + else { + // run $.ajax here + } +}); + + +Let's see how easy it is to check for invalid characters in a username: + + +$("#form").submit(function( e ) { + var inputtedPhoneNumber = $("#phone").val() + , phoneNumberRegex = ^\d*$/; // match only numbers + + if ( !phoneNumberRegex.test( inputtedPhoneNumber ) ) { // if the phone number doesn't match the regex + // usually show some kind of error message ere + return false; // prevent the form from submitting + } + else { + // run $.ajax here + } +}) + @@ -70,6 +88,4 @@ You can pass in an optional argument before the callback function that specifies $.ajaxPrefilter( "json script", function( options, originalOptions, jqXHR ) { // do all of the prefiltering here, but only for requests that indicate a dataType of "JSON" or "script" }) - - -As you can see, prefiltering with jQuery is a simple, yet quite under-utilized, way to modify your requests to your liking. \ No newline at end of file + \ No newline at end of file From 4ed3cd7a2cc3f47b56677ad483d2b97478efe089 Mon Sep 17 00:00:00 2001 From: Connor Montgomery Date: Sun, 19 Feb 2012 22:06:09 -0600 Subject: [PATCH 04/15] remove astray space --- content/getting-started/downloading-jquery.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/getting-started/downloading-jquery.md b/content/getting-started/downloading-jquery.md index 1e5a4aa6..00d1eba3 100644 --- a/content/getting-started/downloading-jquery.md +++ b/content/getting-started/downloading-jquery.md @@ -88,7 +88,7 @@ The minified versions, while having a larger file size than the packed versions ## jQuery Git - An Instant WIP Build For Testing This work-in-progress build (known as jQuery Git) is generated once a minute -from the [ jQuery Git repository ]( http://github.com/jquery/jquery ). It is +from the [jQuery Git repository]( http://github.com/jquery/jquery ). It is provided as a convenience for anyone that wants to help test changes in the next version of jQuery. From d06b2de35851db7396548dbafd04f5955ec66066 Mon Sep 17 00:00:00 2001 From: Connor Montgomery Date: Sun, 19 Feb 2012 22:06:45 -0600 Subject: [PATCH 05/15] fix incorrect bold in markdown --- content/getting-started/downloading-jquery.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/getting-started/downloading-jquery.md b/content/getting-started/downloading-jquery.md index 00d1eba3..9089875c 100644 --- a/content/getting-started/downloading-jquery.md +++ b/content/getting-started/downloading-jquery.md @@ -135,7 +135,7 @@ jQuery currently requires the following components to be installed: ** ant: Available on any platform with JDK and ANT installed * java: A copy of Java, version 1.6.0 or later (required to build the minified version of jQuery). -** Build Process ** +**Build Process** You will now need to use the build system that you chose previously - either make or ant. From c445a834509bf6093a4f158d3434176d0d158e2c Mon Sep 17 00:00:00 2001 From: Connor Montgomery Date: Sun, 19 Feb 2012 22:19:31 -0600 Subject: [PATCH 06/15] added comments to js101/arrays for clarification --- content/javascript-101/arrays.md | 8 ++++---- content/javascript-101/loops.md | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/content/javascript-101/arrays.md b/content/javascript-101/arrays.md index 67f9e791..435d3cd7 100644 --- a/content/javascript-101/arrays.md +++ b/content/javascript-101/arrays.md @@ -24,16 +24,16 @@ console.log(myArray.length); // logs 2 var myArray = [ 'hello', 'world' ]; -myArray[1] = 'changed'; +myArray[1] = 'changed'; // myArray is now now ['hello', 'changed'] var myArray = [ 'hello', 'world' ]; -myArray.push('new'); +myArray.push('new'); // myArray is now ['hello', 'world', 'new'] var myArray = [ 'h', 'e', 'l', 'l', 'o' ]; -var myString = myArray.join(''); // 'hello' -var mySplit = myString.split(''); // [ 'h', 'e', 'l', 'l', 'o' ] +var myString = myArray.join(''); // myString = 'hello' +var mySplit = myString.split(''); // mySPlit = [ 'h', 'e', 'l', 'l', 'o' ] diff --git a/content/javascript-101/loops.md b/content/javascript-101/loops.md index 6e0540c0..f38049af 100644 --- a/content/javascript-101/loops.md +++ b/content/javascript-101/loops.md @@ -128,6 +128,7 @@ the loop's body with the break statement. You may also want to continue the loop without executing more of the loop's body. This is done using the continue statement. + for (var i = 0; i < 10; i++) { if (something) { From 99828d517e3a1ed247ab5fffc1137bcd32bcf2fa Mon Sep 17 00:00:00 2001 From: Connor Montgomery Date: Sun, 19 Feb 2012 22:28:08 -0600 Subject: [PATCH 07/15] change footer from jQuery 1.5.x to jquery 1.7.1. Also add hrefs to download links in footer --- content/javascript-101/objects.md | 7 ++++--- layouts/footer.html | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/content/javascript-101/objects.md b/content/javascript-101/objects.md index a33dee13..4e42b4c2 100644 --- a/content/javascript-101/objects.md +++ b/content/javascript-101/objects.md @@ -12,23 +12,24 @@ called a method of the object. Otherwise, they are called properties. As it turns out, nearly everything in JavaScript is an object -- arrays, functions, numbers, even strings -- and they all have properties and methods. + var myObject = { sayHello : function() { console.log('hello'); }, - myName : 'Rebecca' }; - myObject.sayHello(); // logs 'hello' console.log(myObject.myName); // logs 'Rebecca' + When creating object literals, you should note that the key portion of each key-value pair can be written as any valid JavaScript identifier, a string (wrapped in quotes) or a number: - + + var myObject = { validIdentifier : 123, 'some string' : 456, diff --git a/layouts/footer.html b/layouts/footer.html index c081b45a..cec80809 100644 --- a/layouts/footer.html +++ b/layouts/footer.html @@ -9,12 +9,12 @@

Quick Access

CDN - //ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js + //ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
- Download jQuery 1.5.1: - Minified (29KB) - Unminified (212KB) + Download jQuery 1.7.1: + Minified (29KB) + Unminified (212KB)