Advertisement
salmanff

jlos (JSON LOcal Storage) Simple

Mar 9th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This is a simplified jlos file, without using the filesystem for archiving data.
  3. See jlos.js (http://pastebin.com/zKkZmJqG) for file system related funtionality...
  4.  
  5. jlos just creates an object with methods like save() which store your data into localStorage.
  6. What data? any json data you add to .data... eg..
  7.  var examplevar = new jlos('examplevar')
  8.  From there, you can treat examplevar.data as a regular json object and read write it as you will....
  9.     DATA SHOULD ONLY BE PUT UNDER ".data"!!!!
  10. examplevar.save saves it to localStorage['examplevar']
  11.  
  12. You can use this code freely with attribution under the MIT License... no guarantees of any sort given that it will not launch a nuclear missile if you use the wrong methods.
  13. */
  14. /* Updated March 12 - Added valueAtInit option... When set, if the data is initialised for the first time (ie in the case where the localstorage file doesn't exist yet, examplevar.data is set to this initial value)...(Not yet reflected in the main jlos file
  15. */
  16. function jlos(name, options) {
  17.   this.name = name;
  18.   this.initialize(options);
  19. }
  20.  
  21. jlos.prototype.initialize = function (options) {
  22.  this.meta = options? {'options': options} : {'options':null};
  23.  if (localStorage["jlos-data-"+this.name] && localStorage["jlos-data-"+this.name].length>0){
  24.     this.data = JSON.parse(localStorage["jlos-data-"+this.name]);
  25.  } else if (options && options.valueAtInit) {
  26.     this.data = options.valueAtInit;
  27.  } else {
  28.     this.data = {};
  29.  }
  30. };
  31.  
  32. jlos.prototype.save = function () {
  33.     localStorage["jlos-data-"+this.name]= JSON.stringify(this.data);
  34. };
  35.  
  36. jlos.prototype.remove = function () {
  37.  localStorage.removeItem("jlos-data-"+this.name);
  38.  this.data=null;
  39. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement