Javascript and jQuery intro

15
JavaScript and jQuery, the Atlassian way

description

Quick intro to Javascript and jQuery for training.

Transcript of Javascript and jQuery intro

Page 1: Javascript and jQuery intro

JavaScript and jQuery,the Atlassian way

Page 2: Javascript and jQuery intro

Quick Intro

• Not a Toy language

• Small but powerful

• Has design flaws

• Hits the high notes

Page 3: Javascript and jQuery intro

Key Concepts

• Load and Go

• Loose Typing

• Lambda

• Prototypal Inheritance

• Single Threaded

Page 4: Javascript and jQuery intro

Values

• Numbers– 64-bit floating point, NaN, Infinity

• Strings– UCS-2 (not quite UTF-16), Immutable

• Booleans• Objects

– Hashes

• null• undefined

Page 5: Javascript and jQuery intro

Resources

http://api.jquery.com/https://developer.mozilla.org/en/JavaScripthttp://documentcloud.github.com/backbone/http://documentcloud.github.com/underscore/

Page 6: Javascript and jQuery intro

// Collections!

var $collection = jQuery("a[rel=userprofile]");

// $collection is a jQuery collection.// It can have 0 or more elements.

$collection.addClass("foo");

// API functions apply to all elements in the// collection.

var p = document.getElementById("myUserProfile");var $collection2 = jQuery(p);

// $collection2 is a jQuery collection.// It has 1 element.

Page 7: Javascript and jQuery intro

var $hi = jQuery("<p>Hello, world.</p>");

// $hi is also a jQuery collection,// containing 1 element.

console.log($collection.length);// The number of elements in $collection.

console.log($collection[0]);// The first DOM element in $collection.

Page 8: Javascript and jQuery intro

//methods on the same collection

$collection.attr("hreflang", "fr");// Sets the "hreflang" attribute on *all*// elements in $collection.

$collection.attr("href");// Gets the "href" attribute of the// *first* element in $collection.

Page 9: Javascript and jQuery intro

//script execution and DOM Ready

console.log(  jQuery("input[type=checkbox]").length); // => 0// The page has only loaded enough// elements to get to this <script>.

jQuery(function() {  // This function is called when the page  // has fully loaded.  console.log(    jQuery("input[type=checkbox]").length  ); // => 6});

Page 10: Javascript and jQuery intro

//events

function handleEvent(event) {  console.log(this);  // The element clicked,  // not a jQuery collection

  event.preventDefault();  // Prevent the default action of this  // event.}

$collection.bind("click", handleEvent);// handleEvent is called when any element// in $collection is clicked.

$collection.unbind("click", handleEvent);

Page 11: Javascript and jQuery intro

//when I say stop I mean…

$collection.bind("keydown",  function(event) {    event.stopPropagation();    event.stopImmediatePropagation();    return false;    // Avoid these.    // This breaks the key rule that    // event handlers should be agnostic    // of other event handlers.  });

Page 12: Javascript and jQuery intro

//custom events

$collection.bind("myCustomEvent",  function(event) {    // custom event handler  });

$collection.trigger("myCustomEvent");// Dispatch an event of type// "myCustomEvent"

Page 13: Javascript and jQuery intro

//bind to arbitrary objects

MyApp.ISSUES = {};

jQuery(MyApp.ISSUES).bind("add_issue",  function(newIssue) {    // Handle adding new issues  });

// jQuery(MyApp.ISSUES) is a jQuery// collection containing the JavaScript// object MyApp.ISSUES.

var newIssue = new Issue(newIssueId);jQuery(MyApp.ISSUES).trigger("add_issue",  [newIssue]);// The second argument to trigger() is an// array of arguments to supply listeners.

Page 14: Javascript and jQuery intro

//ajax

var xhr = jQuery.ajax("/users", {  type: "PUT", // HTTP method  contentType: "application/json", // request header  data: '{"name":"Fred Jones","role":"JavaScript Developer"}',  dataType: "json" // Accept: application/json});

xhr.complete(function(xhr) {  console.log(xhr.status); // 200  console.log(xhr.data); // JSON-decoded response  console.log(xhr.responseText); // JSON string});

xhr.success(function(data) {  console.log(data); // JSON-decoded response});

xhr.error(function(xhr, errType) {  console.log(errType); // "error" (4xx or 5xx)  // Other possible values: "abort", "timeout", "parsererror"});

Page 15: Javascript and jQuery intro

//store data against DOM nodes

// Set data:jQuery.data(obj, "key", "value");

// Get data:jQuery.data(obj, "key"); // => "value"

// obj can be any DOM element or// JavaScript object.// The key can be any string.// The value can be any type.