Skip to content

Instantly share code, notes, and snippets.

@danb-humaan
Last active March 21, 2016 07:26
Show Gist options
  • Save danb-humaan/be75ae5ca90268d55d2e to your computer and use it in GitHub Desktop.
Save danb-humaan/be75ae5ca90268d55d2e to your computer and use it in GitHub Desktop.
var ls = require('lib/LocalStorage');
// These examples assume you are using jQuery for AJAX calls.
// Closure usage
ls.getItem('devices', function(devices) {
if (devices === null) {
$.ajax({
url: '/data/devices.json',
dataType: 'json',
method: 'get'
}).done(function (data, textStatus, jqXHR) {
if (jqXHR.status == 200) {
// Cache response in localStorage for 1 hour.
ls.setItem('devices', data.devices, 3600);
anotherFunction(data.devices);
}
});
} else {
anotherFunction(devices);
}
});
// Variable assignment
var devices = ls.getItem('devices', function(devices) {
if (devices === null) {
$.ajax({
url: '/data/devices.json',
dataType: 'json',
method: 'get'
}).done(function (data, textStatus, jqXHR) {
if (jqXHR.status == 200) {
// Cache response in localStorage for 1 hour.
ls.setItem('devices', data.devices, 3600);
return data.devices;
}
});
} else {
return devices;
}
});
doSomething(devices);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment