简体   繁体   English

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

[英]How to store local data in Sencha Touch / PhoneGap?

I have setup a SenchaTouch/PhoneGap app that pulls information from an external XML feed. 我已经设置了一个SenchaTouch / PhoneGap应用程序,该应用程序从外部XML提要中获取信息。 My problem is this will obviously only work when online. 我的问题是,这显然仅在在线时有效。

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,

Add this to the Event model: 将此添加到Event模型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM