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

时间:2022-08-09 19:43:06

I have setup a SenchaTouch/PhoneGap app that pulls information from an external XML feed. My problem is this will obviously only work when online.

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

How do I go about storing the information from the external feed in the local storage to be used offline?

如何将外部源中的信息存储在本地存储中以供脱机使用?

Here is the app data store code:

这是应用程序数据存储代码:

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();

Update after Ilya139's answer:

Ilya139回答后更新:

I've implemented the code, but now my list is empty... :(

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

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();

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();

Model

模型

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

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

});

And the list is set to use the offline store:

并且列表设置为使用脱机存储:

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

1 个解决方案

#1


1  

Add this to the Event model:

将其添加到事件模型:

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

And then for each event that you download call save() like this:

然后为您下载的每个事件调用save(),如下所示:

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

Then to load:

然后加载:

 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'
}});

Update

更新

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

#1


1  

Add this to the Event model:

将其添加到事件模型:

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

And then for each event that you download call save() like this:

然后为您下载的每个事件调用save(),如下所示:

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

Then to load:

然后加载:

 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'
}});

Update

更新

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