28.04.2014 Views

ASP.NET MVC 4 in Action - Manning Publications

ASP.NET MVC 4 in Action - Manning Publications

ASP.NET MVC 4 in Action - Manning Publications

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

106 CHAPTER 7 Ajax <strong>in</strong> <strong>ASP</strong>.<strong>NET</strong> <strong>MVC</strong><br />

7.1.1 jQuery primer<br />

When work<strong>in</strong>g with jQuery, you ma<strong>in</strong>ly work with the jQuery function (primarily<br />

us<strong>in</strong>g the $ alias) that can perform a variety of different operations depend<strong>in</strong>g on its<br />

context. For example, to use jQuery to f<strong>in</strong>d all of the elements on a page and<br />

add a CSS class to each one, you could use the follow<strong>in</strong>g l<strong>in</strong>e of code:<br />

$('div').addClass('foo');<br />

When you pass a str<strong>in</strong>g to the $ function, jQuery will treat it as a CSS selector and<br />

attempt to f<strong>in</strong>d any elements <strong>in</strong> the page that match this selector. In this case, it will<br />

f<strong>in</strong>d all the elements <strong>in</strong> the page. Likewise, call<strong>in</strong>g $('#foo') would f<strong>in</strong>d the<br />

element whose ID is foo, whereas a call to $('table.grid td') would f<strong>in</strong>d all of the<br />

elements nested with<strong>in</strong> tables that have a class of grid.<br />

The result of call<strong>in</strong>g this function is another <strong>in</strong>stance of the jQuery object that<br />

wraps the underly<strong>in</strong>g DOM elements that matched the selector. Because it returns<br />

another jQuery <strong>in</strong>stance, you can cont<strong>in</strong>ue to cha<strong>in</strong> calls to jQuery methods that <strong>in</strong><br />

turn allow you to perform complex operations on DOM elements <strong>in</strong> a very succ<strong>in</strong>ct<br />

manner. The preced<strong>in</strong>g example calls the addClass method, which adds the specified<br />

CSS class to each element conta<strong>in</strong>ed <strong>in</strong> the wrapped set (<strong>in</strong> this example, all of the<br />

elements <strong>in</strong> the page).<br />

You can also attach events to elements <strong>in</strong> a similar fashion. If you wanted to show a<br />

message box when a button was clicked, one approach could be to place the<br />

JavaScript <strong>in</strong>l<strong>in</strong>e <strong>in</strong> an onclick event:<br />

<br />

Click me!<br />

<br />

The downside of this approach is that it mixes code with markup. This can impact the<br />

ma<strong>in</strong>ta<strong>in</strong>ability of your application and make the logic difficult to follow. Us<strong>in</strong>g<br />

jQuery, you can attach an event handler to the button’s click event externally.<br />

Click me!<br />

<br />

$('button#myButton').click(function() {<br />

alert('I was clicked!');<br />

});<br />

<br />

This example <strong>in</strong>troduces a script element with<strong>in</strong> the page to conta<strong>in</strong> the JavaScript<br />

code and tell jQuery to f<strong>in</strong>d any elements with an id of myButton and run<br />

a function when the button is clicked. In this case, the browser will simply display a<br />

message <strong>in</strong>dicat<strong>in</strong>g that the button was clicked.<br />

This approach is known as unobtrusive JavaScript. By keep<strong>in</strong>g the site’s markup separate<br />

from its behavior (code), ma<strong>in</strong>ta<strong>in</strong>ability is improved and it’s easier to follow the<br />

flow of the code.<br />

In the same way that you can attach events to elements, you can also attach a ready<br />

event to the entire page. This event will be fired once the page’s DOM hierarchy has

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!