Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Extending Ajax Events for all mankind


Published on

Presented 9/12/2009 at jQuery Conf 2009.

Like any good Javascript framework would, jQuery includes Ajax functionality into its core. In particular, though, jQuery has some interesting ways it approaches event handling with Ajax responses. We’re going to dive into this Ajax event mechanism, and see what fun we can have with extending it.

This talk will first review the various Ajax events and some common ways they are used. Then, we’ll investigate jQuery source code and see how each is accomplished. Armed with this knowledge, we’ll talk about some scenarios where you might want to extend these events with extra functionality, or even add your own custom Ajax events.

To move from the theoretical into the concrete, we will discuss two jQuery Ajax plugins I released, mpAjax and flXHRproxy, and look at how each needed in its own way to hook into the Ajax event mechanism and extend it.

Published in: Technology, Sports, Business
  • Be the first to comment

Extending Ajax Events for all mankind

  1. 1. welcome
  2. 2. who am i? kyle simpson @getify [email_address]
  3. 3. what is the meaning of life? i dunno. maybe jQuery? or 42. something like that.
  4. 4. so, why are we here? I’m glad you asked.
  5. 5. <ul><li>extending ajax events for all mankind… </li></ul>jQuery Conf 2009
  6. 6. our little chat today <ul><li>what are Ajax events? </li></ul><ul><li>how does jQuery handle Ajax events? </li></ul><ul><li>why would I want to extend them? </li></ul><ul><li>gimme some real code! </li></ul><ul><ul><li>mpAjax </li></ul></ul><ul><ul><li>flXHRproxy </li></ul></ul>
  7. 8. what are Ajax events? events specifically triggered when Ajax requests happen. duh. $([‘Complete’,’Error’,’Send’,’Start’,’Stop’,’Success’]).each(function() { console.log(‘ajax’ + this); }); technically, an event is a signal to jQuery to call one or more registered callback functions to let them know something happened. blah, blah, blah.
  8. 9. how does jQuery handle Ajax events? <ul><li>“ a tale of two cities” </li></ul><ul><ul><li>per-request (aka, “local”) </li></ul></ul><ul><ul><ul><li>callback functions as parameters/options to $.ajax(), $.get(), $.post(), etc </li></ul></ul></ul><ul><ul><li>global events </li></ul></ul><ul><ul><ul><li>listeners bound to DOM element(s) </li></ul></ul></ul><ul><ul><ul><li>triggered with every Ajax call </li></ul></ul></ul>
  9. 10. how does jQuery handle Ajax events? <ul><li>per-request callbacks: </li></ul><ul><ul><li>‘ beforeSend’, ‘complete’, ‘error’, ‘success’, ‘timeout’ </li></ul></ul><ul><ul><li>different function signatures (parameters) </li></ul></ul><ul><ul><li>only one registered callback per event per request </li></ul></ul>
  10. 11. how does jQuery handle Ajax events? <ul><li>global events: </li></ul><ul><ul><li>“ ajax___” where ___ is one of: ‘Complete’,’Error’,’Send’,’Start’,’Stop’,’Success’ </li></ul></ul><ul><ul><li>can be bound to whatever DOM element(s) you choose; all handlers get called for each event </li></ul></ul><ul><ul><li>‘ Start’ and ‘Stop’ are special </li></ul></ul><ul><ul><li>consistent function signature </li></ul></ul><ul><ul><li>can be disabled per-request via ‘global:false’ </li></ul></ul>
  11. 12. … try to take over the Ajax event.
  12. 13. how does jQuery handle Ajax events? <ul><li>so, how can we use them? </li></ul><ul><li>hide/show loading “spinner” </li></ul><ul><li>show generic error message </li></ul><ul><li>resubmit a request on timeout </li></ul>
  13. 14. how does jQuery handle Ajax events? hide/show loading “spinner”:
  14. 15. how does jQuery handle Ajax events? show generic error message:
  15. 16. how does jQuery handle Ajax events? resubmit a request on timeout:
  16. 17. why would I want to extend them? <ul><li>overall goal == better code organization </li></ul><ul><li>existing events not granular enough </li></ul><ul><ul><li>monolithic callback functions with ‘switch’ </li></ul></ul><ul><li>extend (or override) existing functionality </li></ul><ul><ul><li>change callback signature </li></ul></ul><ul><ul><li>additional pre- or post-processing on requests or responses </li></ul></ul>
  17. 18. why would I want to extend them? <ul><li>some extension ideas: </li></ul><ul><li>request retry on timeout </li></ul><ul><li>“ application error” event handling </li></ul><ul><li>smarter response caching </li></ul><ul><li>ordered responses via request/response queue </li></ul><ul><li>solve “travelling salesman” problem </li></ul>
  18. 19. gimme some real code! example: application-level success/error
  19. 20. gimme some real code! example: application-level success/error
  20. 21. mpAjax <ul><li>multi-part Ajax *responses* -- return multiple different &quot;parts&quot; of data (different types, etc) in a single response. </li></ul><ul><li>return a block of HTML (without encoding/escaping it into JSON), and a separate JSON packet with data </li></ul><ul><li>return an HTML template and some JSON data separately, without any obfuscation/encoding/escaping of either </li></ul><ul><li>return some HTML, some JSON, and also some CSS, and handle all three types of data separately </li></ul><ul><li>return two or more different blocks of HTML, and need to handle each separately </li></ul>
  21. 22. mpAjax <ul><li>strategy: </li></ul><ul><li>post-process data from a ‘success’ful Ajax request </li></ul><ul><li>if multi-part format found, string parse response text, send each part to separate response handler </li></ul><ul><li>otherwise, behave exactly like normal Ajax request/response </li></ul>
  22. 23. mpAjax mpAjax jQuery plugin: code: demo:
  23. 24. flXHRproxy <ul><li>flXHR is a javascript API wrapper that mimics native XHR object in almost every way, but behind the scenes, makes calls via an invisible flash asset (crossdomain.xml) </li></ul><ul><li>flXHRproxy adapts jQuery to use flXHR for fully featured cross-domain Ajax </li></ul><ul><ul><li>uses the jQuery plugin ‘XHR’ to register flXHR as a XHR clone </li></ul></ul><ul><ul><li>registers/associates specific flXHR config options with a specific target URL </li></ul></ul>
  24. 25. flXHRproxy <ul><li>strategy: </li></ul><ul><li>register flXHR as XHR drop-in replacement </li></ul><ul><li>extend ‘success’ function signature (parameters) to include the XHR object </li></ul><ul><li>tie flXHR’s ‘onerror’ event to trigger the jQuery ‘error’ events </li></ul>
  25. 26. flXHRproxy flXHR: flXHRproxy jQuery plugin: code: demo:
  26. 27. enjoy rate this talk: