Skip to content

Instantly share code, notes, and snippets.

@moneal
Created December 14, 2012 04:22
Show Gist options
  • Save moneal/4282681 to your computer and use it in GitHub Desktop.
Save moneal/4282681 to your computer and use it in GitHub Desktop.
Populating datasets from localstorage
if (Modernizr.localstorage) {
//console.log("Can use localstorage");
var clients = localStorage.getItem("clients");
// console.log(clients);
if ( clients == null ) {
// Fetch the clients list
$.getJSON('/api/client_list', function(data) {
localStorage.setItem("clients", JSON.stringify( data ));
populate_autocomplete( data );
});
} else {
// Use what we have
populate_autocomplete( JSON.parse( clients ) );
}
} else {
// console.log("Can't store local :(");
}
function populate_autocomplete( clients ) {
// console.log( clients )
$.each( clients, function( index, value ) {
// console.log(index + ': ' + value);
$('#accounts-list').append('<option value="' + value + '"/>')
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment