如何在Sencha Touch / PhoneGap中存储本地数据

How to store local data in Sencha Touch / PhoneGap?

本文关键字:存储 数据 PhoneGap Sencha Touch      更新时间:2023-09-26

我设置了一个SenchaTouch/PhoneGap应用程序,从外部XML提要提取信息。我的问题是,这显然只能在在线时工作。

如何将来自外部提要的信息存储在本地存储中以供离线使用?

下面是应用数据store代码:

App.eventstore = new Ext.data.Store({
    model: 'Event',
    sorters: 'title',
    autoLoad: true,
    getGroupString : function(record) {
        return record.get('title')[0];
    },
    proxy: {
        type: 'ajax',
        url: 'http://the-url-to-the-file.xml',
        reader: {
            idProperty: 'id',
            type: 'xml',
            root: 'events',
            record: 'event'
        }
    }
});
App.eventstore.read();

在Ilya139的回答后更新:

我已经实现了代码,但现在我的列表是空的…(

App.eventstore = new Ext.data.Store({
    model: 'Event',
    sorters: 'title',
    autoLoad: true,
    getGroupString : function(record) {
        return record.get('title')[0];
    },
    proxy: {
        type: 'ajax',
        url: 'http://the-url-to-the-file.xml',
        reader: {
            idProperty: 'id',
            type: 'xml',
            root: 'events',
            record: 'event'
        }
    }
});
App.eventstore.read();
App.eventstore.each(function(record){record.save();});
App.offlineeventstore = new Ext.data.Store({
    model: 'Event',
    sorters: 'title',
    autoLoad: true,
   getGroupString : function(record) {
    return record.get('title')[0];
   },
   proxy: {
      type: 'localstorage',
      id:'events'
    }
});
App.offlineeventstore.read();

Ext.regModel('Event', {
    fields: [
        {name: 'id', mapping: '@id', type: 'integer'},
        {name: 'title', type: 'string'},
        etc etc...
    ],
    proxy: {
       type: 'localstorage',
       id:'events'
    }
});

列表设置为使用离线商店:

items: [{
        xtype: 'list',
        store: App.offlineeventstore,
        itemTpl: '{title}',
        grouped: true,
        indexBar: true,

将此添加到Event模型:

 proxy: {
       type: 'localstorage',
       id:'events'
    }

然后对于你下载的每个事件,像这样调用save():

 App.eventstore.each(function(record){record.save();});

然后加载:

 App.offlinestore = new Ext.data.Store({
     model: 'Event',
    sorters: 'title',
    autoLoad: true,
   getGroupString : function(record) {
    return record.get('title')[0];
   },
   proxy: {
       type: 'localstorage',
      id:'events'
}});

App.eventstore.load(function(){
   App.eventstore.each(function(record){record.save();});
   offlineeventstore.load();
});