Extending Ajax Events for all mankind
-
Upload
kyle-simpson -
Category
Technology
-
view
4.729 -
download
4
description
Transcript of Extending Ajax Events for all mankind
welcome
what is the meaning of life?
i dunno. maybe jQuery?
or 42. something like that.
so, why are we here?
I’m glad you asked.
extending ajax events for all
mankind…
jQuery Conf 2009
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!o mpAjaxo flXHRproxy
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.
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
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
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’
…try to take over the Ajax event.
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
how does jQuery handle Ajax events?
hide/show loading “spinner”:
how does jQuery handle Ajax events?
show generic error message:
how does jQuery handle Ajax events?
resubmit a request on timeout:
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
why would I want to extend them?
some extension ideas:
1. request retry on timeout
2. “application error” event handling
3. smarter response caching
4. ordered responses via request/response queue
5. solve “travelling salesman” problem
gimme some real code!example: application-level success/error
gimme some real code!example: application-level success/error
mpAjax
multi-part Ajax *responses* -- return multiple different "parts" of data (different types, etc) in a single response.
1. return a block of HTML (without encoding/escaping it into JSON), and a separate JSON packet with data
2. return an HTML template and some JSON data separately, without any obfuscation/encoding/escaping of either
3. return some HTML, some JSON, and also some CSS, and handle all three types of data separately
4. return two or more different blocks of HTML, and need to handle each separately
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
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
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
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
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
enjoy
http://plugins.jquery.com/project/mpAjaxhttp://test.getify.com/mpAjax
http://plugins.jquery.com/project/flXHRhttp://flxhr.flensed.com/jquery.php
rate this talk: http://speakerrate.com/talks/1408-extending-ajax-events-for-all-mankind