Extending Ajax Events for all mankind

  • 4,504 views
Uploaded on

Presented 9/12/2009 at jQuery Conf 2009. …

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.

More in: Technology , Sports , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,504
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
100
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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