how to work with json feed stored in localStorage in phonegap app?

前端 未结 2 2008
独厮守ぢ
独厮守ぢ 2021-02-06 12:46

Here\'s what I am doing,

Get request to my web server, response is in json. Using jquery templates to render that callback data in my app. Pretty straightforward, works

2条回答
  •  太阳男子
    2021-02-06 13:26

    App.local = (function () {
      var self = {};
    
      self.get = function (key) {
        var b = localStorage.getItem(key);
        return b ? JSON.parse(b) : null;
      }
    
      self.set = function (key, value) {
        var b = JSON.stringify(value);
        localStorage.setItem(key, b);
      }
    
      return self;
    })();
    

    Now you have a nice interface to local storage,

    var local = App.local;
    local.set('foo', 'bar');
    var bar = local.get('foo');
    console.log(bar);
    

提交回复
热议问题