Created
November 19, 2013 21:24
-
-
Save maxxcrawford/7552845 to your computer and use it in GitHub Desktop.
AJAX Call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cache = {}; | |
function getData( val ){ | |
// Return a promise from the cache (if available) | |
// or create a new one (a jqXHR object) and store it in the cache. | |
var promise = cache[val]; | |
if (!promise) { | |
promise = $.ajax('/foo/', { | |
data: { value: val }, | |
dataType: 'json' | |
}); | |
cache[val] = promise; | |
} | |
return promise; | |
} | |
$.when(getData('foo')).then(function(resp){ | |
// do something with the response, which may | |
// or may not have been retrieved using an | |
// XHR request. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment