0

我正在尝试离线存储数据,但是在这里我想将数据存储在本地存储中,没有存储能够存储它,所有值在本地存储中都变为空。

这是基于; http://www.robertkehoe.com/2012/11/sencha-touch-2-localstorage-example/

型号: * Online.js *

Ext.define('default.model.Online', {
  extend: 'Ext.data.Model',
  config: {
    fields: [
                    'cat_id',
                    'category_name'
                ]
  }
});

离线.js

Ext.define('default.model.Offline', {
  extend: 'Ext.data.Model',
  config: {
    fields: [
                    'cat_id',
                    'category_name'
                ],
    identifier:'uuid', // IMPORTANT, needed to avoid console warnings!
    proxy: {
      type: 'localstorage',
      id  : 'category'
    }
  }
});

商店:

Ext.define('default.store.News', {
  extend:'Ext.data.Store',


  config:{
    model:'default.model.Online',
    proxy: {
      timeout: 3000, // How long to wait before going into "Offline" mode, in milliseconds.
      type: 'ajax',
      url: 'http://alucio.com.np/trunk/dev/sillydic/admin/api/word/categories/SDSILLYTOKEN/650773253e7f157a93c53d47a866204dedc7c363?_dc=1376475408437&page=1&start=0&limit=25' , // Sample URL that simulates offline mode. Example.org does not allow cross-domain requests so this will always fail
      reader: {
                type: "json",
                rootProperty: "data"
            }
      },
    autoLoad: true
  }
});

控制器:

Ext.define('default.controller.Core', {
  extend : 'Ext.app.Controller',

  config : {
    refs    : {
      newsList   : '#newsList'
    }
  },
  init : function () {
    var onlineStore = Ext.getStore('News'),
      localStore = Ext.create('Ext.data.Store', {
        model: "default.model.Offline"
      }),
      me = this;

    localStore.load();

    onlineStore.on('refresh', function (store,record) {
     Ext.Msg.alert('Notice', 'You are in online mode', Ext.emptyFn);
      //  console.dir(record.data.name);
        console.dir(record.get('category_name'));
        console.log(record.items[0].raw.category_name);
        console.log(record.get('category_name'));
      // Get rid of old records, so store can be repopulated with latest details
      localStore.getProxy().clear();

      store.each(function(record) {

        var rec = {
          name : record.data.category_name+ ' (from localStorage)' // in a real app you would not update a real field like this!
        };

        localStore.add(rec);
        localStore.sync();
      });

    });
      onlineStore.getProxy().on('exception', function () {
      me.getNewsList().setStore(localStore); //rebind the view to the local store
      localStore.load(); // This causes the "loading" mask to disappear
      Ext.Msg.alert('Notice', 'You are in offline mode', Ext.emptyFn); //alert the user that they are in offline mode
    });

  }
});

我想,我没有从这个record.data.category_nam中获得价值。在这里,我得到了第一个值:record.items[0].raw.category_name。那么如何存储在本地存储中。和查看文件:

Ext.define('default.view.Main', {
  extend : 'Ext.List',

  config : {
    id               : 'newsList',
    store            : 'News',
    disableSelection : false,
    itemTpl          : Ext.create('Ext.XTemplate',
      '{category_name}'
    ),
    items            : {
      docked : 'top',
      xtype  : 'titlebar',
      title  : 'News List'
    }
  }
});

在本地存储中,以下输出:

category-5ea01a8d-ef1e-469e-8ec4-790ec7306aaf
{"cat_id":null,"category_name":null,"id":"5ea01a8d-ef1e-469e-8ec4-790ec7306aaf"}
category-f3e090dd-8f25-4b20-bb6e-b1a030e07900
{"cat_id":null,"category_name":null,"id":"f3e090dd-8f25-4b20-bb6e-b1a030e07900"}
category-5148e6eb-85ae-4acd-9dcd-517552cf5d97
{"cat_id":null,"category_name":null,"id":"5148e6eb-85ae-4acd-9dcd-517552cf5d97"}
category-ec23ff8b-1faa-4f62-9284-d1281707a9bc
{"cat_id":null,"category_name":null,"id":"ec23ff8b-1faa-4f62-9284-d1281707a9bc"}
category-6c1d

我有显示,但无法存储在本地存储中以供离线建议。我做错了,我无法得到它。

4

1 回答 1

0

您创建的记录应与SF.model.Offline模型匹配。

在您的以下代码中

 var rec = {
  // name is the field name of the `SF.model.Offline` model.
  name : record.data.category_name+ ' (from localStorage)' 
 };

 localStore.add(rec);
 localStore.sync();

但是您会看到模型中没有名为 name 的字段SF.model.Offline

这是你应该做的

楷模

Ext.define('SF.model.Online', {
    extend : 'Ext.data.Model',    
    config: {
        fields: ['cat_id','category_name'],
   }
});

Ext.define('SF.model.Offline', {
    extend : 'Ext.data.Model',
    config: {
        fields: ['cat_id','category_name'],
        identifier:'uuid', 
        proxy: {
          type: 'localstorage',
          id  : 'category'
        }
    }
});

店铺

Ext.define('SF.store.Category', {
    extend : 'Ext.data.Store',   
    config : {
        model : 'SF.model.Online',
        storeId : 'category',
        proxy: {
            timeout: 3000, 
            type: 'ajax',
            url: 'same url' , 
        reader: {
                type: "json",
                rootProperty: "data"
            }
      },
        autoLoad : true
    }
});

在控制器中

 var onlineStore = Ext.getStore('category'),
 localStore = Ext.create('Ext.data.Store', { 
        model: "SF.model.Offline"
    }),
    me = this;

  localStore.load();  

  onlineStore.on('refresh', function (store, records) {  
    localStore.getProxy().clear();
    onlineStore.each(function(record) {

     //You creating record here, The record fields should match SF.model.Offline model fields
      var rec = {
        cat_id : record.data.cat_id + ' (from localStorage)',
        category_name : record.data.category_name + ' (from localStorage)'
      };

      localStore.add(rec);
      localStore.sync(); 
    });
  });  

  onlineStore.getProxy().on('exception', function () {
    me.getNewsList().setStore(localStore); 
    localStore.load(); 
    Ext.Msg.alert('Notice', 'You are in offline mode', Ext.emptyFn);
 });
于 2013-08-19T07:03:55.153 回答