Book Image

Mastering jQuery

By : Alex Libby
Book Image

Mastering jQuery

By: Alex Libby

Overview of this book

<p>Mastering jQuery has been written not only to help maximize your skills with core functionality in the library, but also to explore some of the more intriguing ways of using the library to achieve real-world solutions that could feature on any website or online environment.</p> <p>You'll start with a look at some of the more advanced ways to incorporate the library into your pages, followed by working with forms and advanced form validation using regular expressions. Next you'll move on to animating in jQuery, advanced event handling, and using jQuery effects.</p> <p>Finally, you will develop practical examples of using jQuery with external functionality such as node-webkit, before finishing with a session on optimizing your version of the library for maximum efficiency and exploring best practices for using QUnit.</p>
Table of Contents (21 chapters)
Mastering jQuery
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Controlling content with jQuery's Promises


Promises, promises – how many times have I heard that phrase, I wonder?

Unlike in real life, when promises made are often broken, we can always guarantee that Promises made in jQuery will be satisfied at some point. Granted, the answer may not always be positive one, but yes, there will at least be a response to a Promise.

A question though, I hear you ask – why, if most events already have callback options built in, do we need to use jQuery's .promises()?

The simple answer is that we have far more control over constructing and reading Promises. For example, we can set a single callback that can be applied to multiple Promises; we can even set a Promise to only fire once, if needed! The beauty though is that using Promises makes it easier to read the code, and chain multiple methods together:

var myEvent = function(){
  return $(selector).fadeIn('fast').promise();
};
$.when( myEvent()).done( function(){
  console.log( 'Task completed.' );
});

We can...