Phonegap:无法使localStorage持久化

时间:2012-06-25 22:40:30

标签: cordova persistence local-storage persist lawnchair

所以我做了一切:

var Store = new Lawnchair({table:'mightapp', adaptor:'gears'}, 
    function(s) {
    var obj = {msg: 'hooray!'};

    s.save({key: "msg", value: obj});
});

然后我注释掉该消息以测试它是否持续...

var Store = new Lawnchair({table:'mightapp', adaptor:'gears'}, 
    function(s) {
    //var obj = {msg: 'hooray!'};

    //s.save({key: "msg", value: obj});
});

它没有 - 我做错了什么?我正在制作一个健身应用程序,我需要非常永久地坚持人们的锻炼/锻炼统计...我应该做一些文件i / o吗?

2 个答案:

答案 0 :(得分:2)

dom适配器对我来说很好。以下是我用于在应用程序重新启动时保留数据的代码。

<script type="text/javascript">
    var store = new Lawnchair({
        adapter: "dom",
        name: "testing"
    }, function(store) {
    });

    store.exists('dhaval', function(available){
        var preStr = "";

        // check whether required data is available, if not create it
        if(available){
            preStr = "data is already available, ";
        }else{
            preStr = "data is not available, ";
            var me = {
                key: 'dhaval'
            };

            // save it
            store.save(me);
        }

        // access it later... even after app restart!
        store.get('dhaval', function(me) {
            $("#data").html(preStr + JSON.stringify(me));
        });
    });
</script>

完整来源检查git repo android-cordova-lawnchair

答案 1 :(得分:1)

尝试将适配器切换为“dom”,因为我认为浏览器中不再支持“齿轮”。