02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

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.

C H A P T E R 8<br />

■ ■ ■<br />

Editing the Calendar<br />

with AJAX <strong>and</strong> <strong>jQuery</strong><br />

Now that your app can display event data without a page refresh, you can see the added convenience<br />

provided <strong>by</strong> AJAX in web applications. Historically, one of the biggest pitfalls of using web apps has been<br />

the fact that each action, no matter how small, usually required waiting for the page to refresh while the<br />

setting was saved. Web apps were convenient when a user needed access to his information on a shared<br />

computer, but the slow workflow was usually enough to make users lean toward desktop applications<br />

whenever possible.<br />

However, with mainstream acceptance <strong>and</strong> use of AJAX, users can now make changes rapidly<br />

without constantly waiting for a page to reload. This makes web apps feel more like desktop apps, which<br />

also makes them much more appealing to users.<br />

In this chapter, you’ll learn to add scripts that make the editing controls for administrators function<br />

smoothly without requiring a page refresh for each action. The only action that will require a page<br />

refresh is the login, since that requires changes to the session.<br />

■ Note Before starting the exercises in this chapter, please log in to the calendar application. The default login<br />

relies on a username of testuser <strong>and</strong> a password of admin.<br />

Opening the Event Creation Form<br />

To start, you’ll modify the script to let administrators add new events without a page refresh. Open<br />

init.js <strong>and</strong> select the button to add new events <strong>by</strong> its class (admin). Add a click event h<strong>and</strong>ler that<br />

prevents the default action <strong>and</strong> (for now) logs a message to confirm it fired properly:<br />

<strong>jQuery</strong>(function($){<br />

var processFile = "assets/inc/ajax.inc.php",<br />

fx = {<br />

"initModal" : function() {...},<br />

"boxin" : function(data, modal) {...},<br />

"boxout" : function(event) {...}<br />

}<br />

$("li>a").live("click", function(event){...});<br />

263

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

Saved successfully!

Ooh no, something went wrong!