Skip to content

Instantly share code, notes, and snippets.

@fer-ri
Created March 17, 2014 12:06
Show Gist options
  • Save fer-ri/9598120 to your computer and use it in GitHub Desktop.
Save fer-ri/9598120 to your computer and use it in GitHub Desktop.
Cordova Phonegap Using Local Storage to Keep Application Settings
Storage.prototype.setArray = function(key, obj) {
return this.setItem(key, JSON.stringify(obj))
}
Storage.prototype.getArray = function(key) {
return JSON.parse(this.getItem(key))
}
var gatheredPosts = new Array();
// let's imagine these "posts" come from a remote data call which has just returned
for (var i = 0; i < posts.length; i++) {
gatheredPosts.push({title:posts[i].title, url:posts[i].url, content:posts[i].content});
}
window.localStorage.setArray("cachedPosts", gatheredPosts);
gatheredPosts = window.localStorage.getArray("cachedPosts");
window.localStorage.setItem("key", "value");
var keyname = window.localStorage.key(i);
// keyname is now equal to "key"
var value = window.localStorage.getItem("key");
// value is now equal to "value"
window.localStorage.removeItem("key");
window.localStorage.setItem("key2", "value2");
window.localStorage.clear();
// localStorage is now empty
// $(function() {
// FastClick.attach(document.body);
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment