jquery presentation

30
jQuery Brownbag Rod Johnson

Upload: rod-johnson

Post on 17-May-2015

3.638 views

Category:

Technology


8 download

DESCRIPTION

Basics about jQuery presented to Ancestry.com

TRANSCRIPT

Page 1: jQuery Presentation

jQuery BrownbagRod Johnson

Page 2: jQuery Presentation

Checking for Null Values

C# Way… JavaScript Way

Page 3: jQuery Presentation

Setting Default Values

C# Way… JavaScript Way

Page 4: jQuery Presentation

Properties

C# Way… JavaScript Way

Page 5: jQuery Presentation

Why is jQuery Awesome?

What jQuery does well:

1. Simplified AJAX

2. DOM Manipulation

3. Event Management

4. Animations

5. Normalizes Browser Differences

Why It Is Successful:

6. Well Designed

7. Easy to extend with plugins

8. Great Documentation

9. Large jQuery Community

10. Cross-browser

Page 6: jQuery Presentation

jQuery Founder

Page 7: jQuery Presentation

jQuery Philosophy

Find some stuff

Do something to it

Page 8: jQuery Presentation

Chaining

Page 9: jQuery Presentation

Example: Conditional Hide Show

Page 10: jQuery Presentation

Selecting Elements: Basic CSS SelectorsSelector Description

* Matches any element

E Matches all elements with tag name E

E F Matches all elements with tag name F that are descendants of E

E>F Matches all elements with tag name F that are direct children of E

E+F Matches all elements F immediately proceeded by sibling E

E~F Matches all elements F preceded by any sibling E

E:has(F) Matches all elements E that have at least one descendant F

E.C Matches all elements E with class name C

E#I Matches element E with id of I

E[A] Matches all elements E with attribute A of any value

E[A=V] Matches all elements E with attribute A whose value is exactly V

E[A^=V] Matches all elements E with attribute A whose value begins with V

E[A$=V] Matches all elements E with attribute A whose value ends with V

E[A*=V] Matches all elements E with attribute A whose value contains V

Page 11: jQuery Presentation

Selecting Elements: Positional SelectorsSelector Description Example

:first The first match of the page. li a:first

:last The last match of the page. li a:last

:first-child The first child element. li:first-child

:last-child The last child element. li:last-child

:only-child Returns all elements that have no siblings. li:only-child

:nth-child(n) The nth child element. li:nth-child(2)

:nth-child(even|odd)

Even or odd children. li:nth-child(even)

Page 12: jQuery Presentation

Example: Expanding Form

Page 13: jQuery Presentation

Example: Expanding Form - Continued

You can chain jQuery objects to each other. This makes for really succinct statements.

Since this item is added dynamically, you will need to use the live() method instead of bind()

Iterate through jQuery wrapped set by using the each() method.

Control chaining with the end() method

Indent your code so you can easily see what is the current object in the chain

There are 2 ways to pass in this data.

Controls the context. This is consistent.

Page 14: jQuery Presentation

Events in jQuery

Control how this event gets bubbled down the DOM

Prevent the default behavior of an element. Useful for buttons and links.

Bind an element that is already added to the DOM

Bind an element that is not yet added to the DOM. Very useful with Ajax or adding elements dynamically

Namespacing works really well when you have multiple listeners attached to the same element.

Unbinding events.

Opposite of live()

Page 15: jQuery Presentation

Example: Pager

Page 16: jQuery Presentation

Example: Pager - Continued

Setup event handler

Optional second parameter stores your custom data that is passed from the event

Load custom data from the data attribute of the html element

Always prevent the default behavior of any hyperlink event you trap.

Events can be triggered by using the trigger() method.

Page 17: jQuery Presentation

Example: Default Images

Bind default resources on the “error” event.

Page 18: jQuery Presentation

jQuery in Visual Studio

By adding script references to your .js file, you get intellisense… kinda.

Good practice to always alias your $ by using Local Scope. This will prevent any collisions with any other library that may use $ variable

You will not be able to select any elements until the document is ready.

Most if your work will happen here.

Page 19: jQuery Presentation

Example: Simulated Page Lifecycle

By adding script references to your .js file, you get intellisense… kinda.

Works good if you have multiple js files that all depend on the document loaded event

Page 20: jQuery Presentation

Example: Ajax Proxy

Very flexible way to pass in parameters to a function.

Pass in as many or as few parameters as you need.

Alias your functions by using jQuery as $

Page 21: jQuery Presentation

Example: Ajax Dropdown with ASP.NET MVC

Page 22: jQuery Presentation

Pattern: Default Values

Old way…

jQuery Way…

Doesn’t scale very well at all.

Pass in an object hash with as many or as few parameters as you want.

Enumerate any default values

Default values will be overwritten by the values you passed in the ‘params’ object.

Page 23: jQuery Presentation

jQuery Plugins

Page 24: jQuery Presentation

jQuery UI Plugin

Page 25: jQuery Presentation

jQuery - BBQ

http://benalman.com/projects/jquery-bbq-plugin/

• Merge query string fragments into the query string

#foo=1&bar=2

• Maintains history with each hash change

• Great for AJAX history management– Paging– Tabs

Page 26: jQuery Presentation

Linq to jQuery

Without Linq to jQuery… With Linq to jQuery…

Page 27: jQuery Presentation

Plugin Authoring

Don’t break the chain…

Can use as if it were just a regular jQuery method

Page 28: jQuery Presentation

Learn More About jQuery

Page 29: jQuery Presentation

jQuery Documentation

Page 30: jQuery Presentation

Learn More

• http://jquerylist.com/

• http://enterprisejquery.com

• http://jquery.com