Last active
August 29, 2015 14:23
-
-
Save ozio/4831e25af55b02c83e12 to your computer and use it in GitHub Desktop.
lscache
This file contains 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
'use strict'; | |
var $ = require('jquery'); | |
var lscache = require('lscache'); | |
var Settings = require('./settings'); | |
var createUrl = function(url, data) { | |
return url + '?' + $.param(data); | |
}; | |
var call = function(method, url, data, cached) { | |
if (cached) { | |
var cachedKey = createUrl(url, data); | |
var cachedResponse = lscache.get(cachedKey); | |
if (cachedResponse) { | |
return $.Deferred().resolve(cachedKey); | |
} | |
} | |
var request = { | |
url: Settings.API_URL + url, | |
data, | |
type: method, | |
timeout: Settings.API_TIMEOUT | |
}; | |
var xhr = $.ajax(request); | |
xhr.always(function(res, status, message) { | |
if (cached) { | |
lscache.set(createUrl(url, data), res, 10); | |
} | |
}); | |
return xhr; | |
}; | |
module.exports = { | |
getStuff: function(that) { | |
call('GET', '/stuff', { that }, true); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment