Permalink
Browse files

Returns the jqXHR for AJAX requests, and returns a jQuery.Defered().p…

…romise for cached responses
  • Loading branch information...
1 parent 6c00d40 commit 93c9791569890c3c46740b56d3a322eee95df4d7 Craig Spaeth committed Dec 13, 2011
Showing with 10 additions and 5 deletions.
  1. +10 −5 lib/jquery.offline.js
View
@@ -146,7 +146,7 @@
// store the result in the cache. This function will be
// deferred until later if the user is offline
function getData() {
- getJSON(url, data, function(json, status) {
+ return getJSON(url, data, function(json, status) {
if ( status == 'notmodified' ) {
// Just return if the response has a 304 status code
return false;
@@ -179,15 +179,20 @@
// If there is anything in the cache, call the callback
// right away, with the "cached" status string
if( text ) {
- var response = fn( $.parseJSON(text), "cached", { cachedAt: date } );
- if( response === false ) return false;
+ var obj = $.parseJSON(text);
+ var response = fn( obj, "cached", { cachedAt: date } );
+ if( response === false ) {
+ var dfd = $.Deferred().promise();
@krisselden
krisselden Jan 5, 2012

How does the promise from this Deferred ever get resolved?

+ dfd.done = function(callback) { callback(obj) };
+ return dfd;
+ }
}
// If the user is online, make the Ajax request right away;
// otherwise, make it the most recent callback so it will
// get triggered when the user comes online
if (window.navigator.onLine) {
- getData();
+ return getData();
} else {
mostRecent = getData;
}
@@ -209,4 +214,4 @@
$.clearJSON = $.noop;
}
-})(jQuery);
+})(jQuery);

0 comments on commit 93c9791

Please sign in to comment.