列表在启动时不会在LocalStorage中显示项目

时间:2015-05-05 16:36:27

标签: sencha-touch sencha-touch-2 local-storage

使用Sencha Touch 2.4.1,我有一个显示货币的List,一个FormPanel,用户可以在其中添加新货币。商店“预先填充”有两个项目,它们在列表中正确显示。当用户添加新项目时,它们也会在列表中正确显示。

但是当应用程序重新启动时,我又回到了正方形 - 只有“预填充”项目显示在列表中 - 上次应用程序执行时添加的任何项目都已消失。虽然我可以在Google Chrome的开发者工具的本地存储中看到它们...为什么它们不会显示在我的列表中?

STORE

Ext.define('DebtManager.store.Currency', {
    extend : 'Ext.data.Store',

    requires : [
        'DebtManager.model.Currency',
        'Ext.data.proxy.LocalStorage'
    ],

    config : {
        autoLoad: true,
        model: 'DebtManager.model.Currency',
        storeId: 'CurrencyStore',

        sorters: [{
            property: 'title',
            direction: 'ASC'
        }],

        proxy: {            
            type: 'localstorage',
            model: 'DebtManager.model.Currency',
            id: 'currenciesLocalstore', // The name of the LocalStorage

            reader: {
                type: 'json',
                rootProperty: 'currencies'
            }            
        },


        data: [
            {
                id: 1,   
                title: 'USD',
                dollarExchangeRate: '1.0'
            },
            {
                id: 2,   
                title: 'SEK',
                dollarExchangeRate: '8.4'
            }
        ]        
    },


    load : function (store, records, success, opr) {
        console.log('Store loaded.');   
    },

    beforeSync : function (options, eOpts) {
        console.log('Syncing store...');   
    },

    addrecords : function( store, records, eOpts ){
        console.log('Record(s) added to store');
    }

});

列表面板

Ext.define('DebtManager.view.CurrenciesListPanel', {
    extend: 'Ext.tab.Panel',
    xtype: 'panel',

    requires: [
        'DebtManager.store.Currency',       
    ],

    config: {
        title: 'Currencies',
        iconCls: 'action',  

        layout: 'fit',

        items: [
            {
                docked: 'top',      
                xtype: 'titlebar',
                title: 'Currencies tab',
                items: [
                    { xtype: 'spacer' },
                    {
                        id: 'newNoteButton',
                        text: 'Add',
                        ui: 'action',

                        handler: function () {                          
                            console.log('Add currency');
                            var editorPanel = Ext.create('DebtManager.view.CurrencyEditorPanel');
                            var currency = Ext.create( 'DebtManager.model.Currency', {title: 'xxx', dollarExchangeRate: '100'} );                           
                            editorPanel.setRecord( currency );                          
                            Ext.Viewport.setActiveItem( editorPanel, {type: 'slide', direction: 'left'});                           
                        }
                    }
                ]               
            },
            Ext.create('Ext.List', {
                id: 'currenciesListPanelList',
                title: 'Currencies list',
                store: Ext.create('DebtManager.store.Currency'), 
                itemTpl: '<div class="list-item-title">{title}</div>' + '<div class="list-item-narrative">{dollarExchangeRate}</div>',

                onItemDisclosure: function (record) {
                    // Edit item
                    console.log('Edit item');
                },

                show: function (list, opts) {
                    console.log('List Shown: ' + list);
                }

            })
        ],        

    },

});

EDITOR PANEL

Ext.define('DebtManager.view.CurrencyEditorPanel', {
    extend: 'Ext.form.FormPanel',

    requires: [
        'DebtManager.store.Currency',
    ],

    config: {        
        fullscreen: true,
        title: 'Edit currency',

        items: [
            {
                docked: 'top',      
                xtype: 'toolbar',
                title: 'Edit currency',
                items: [
                    {
                        id: 'backButton',
                        text: 'Back',
                        ui: 'back',

                        handler: function () {                          
                            Ext.Viewport.remove(Ext.Viewport.getActiveItem(), true); // Remove this panel                           
                        }
                    },
                    { xtype: 'spacer' },
                    {
                        id: 'saveButton',
                        text: 'Save',
                        ui: 'action',

                        handler: function () {                          
                            console.log('Save');

                            var editor = Ext.Viewport.getActiveItem(); 
                            var currentCurrency = editor.getRecord();
                            editor.updateRecord( currentCurrency );



                            var store = Ext.data.StoreManager.lookup('CurrencyStore');

                            if (store.findRecord('id', currentCurrency.getData().id) === null) {
                                console.log('Adding record to store...');
                                console.log( currentCurrency );
                                store.add(currentCurrency);
                            } else {
                                console.log('Marking record as dirty...');
                                currentCurrency.setDirty();
                            }

                            store.sync();


                            Ext.getCmp('currenciesListPanelList').refresh();

                            Ext.Viewport.remove(Ext.Viewport.getActiveItem(), true); // Remove this panel                           

                        }
                    }
                ]               
            },
            {
                xtype: 'textfield',
                name: 'id',
                label: 'ID',
                required: true
            },            
            {
                xtype: 'textfield',
                name: 'title',
                label: 'Title',
                required: true
            },            
            {
                xtype: 'textfield',
                name: 'dollarExchangeRate',
                label: 'USD Exchange Rate',
                required: true
            },
            {
                docked: 'bottom',      
                xtype: 'toolbar',                
                items: [
                    { xtype: 'spacer' },
                    {
                        id: 'deleteButton',
                        iconCls: 'trash',
                        iconMask: true,

                        handler: function () {                          
                            console.log('Delete');
                        }
                    }
                ]               
            },

        ]
    }
});

MODEL

Ext.define('DebtManager.model.Currency', {
    extend: 'Ext.data.Model',

    requires: [
    ],

    config: {
        idProperty: 'id',        
        identifier: { 
            type: 'uuid',
            isUnique : true 
        }, 

        fields: [
            {
                name: 'title',
                type: 'string'
            },
            {
                name: 'dollarExchangeRate',
                type: 'string'
            }
        ],
    }
});

Google Chrome中的LocalStorage

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要在商店和列表中更改一些配置。

商店中的更改

  1. 加载,beforeSync和addrecords是监听器所以,将它放在监听器中
  2. 听众应该在config
  3. 不要在里面重复model: 'DebtManager.model.Currency' 代理
  4. 更新商店

    Ext.define('DebtManager.store.Currency', {
        extend : 'Ext.data.Store',
    
        requires : [
            'DebtManager.model.Currency',
            'Ext.data.proxy.LocalStorage'
        ],
    
        config : {
            autoLoad: true,
            model: 'DebtManager.model.Currency',
            storeId: 'CurrencyStore',
    
            sorters: [{
                property: 'title',
                direction: 'ASC'
            }],
    
            proxy: {            
                type: 'localstorage',
                id: 'currenciesLocalstore', // The name of the LocalStorage
            },
    
    
            listeners : {
                load : function (store, records, success, opr) {
                    console.log('Store loaded.');   
                },
    
                beforeSync : function (options, eOpts) {
                    console.log('Syncing store...');   
                },
    
                addrecords : function( store, records, eOpts ){
                    console.log('Record(s) added to store');
                }        
            }
        }
    });
    

    列表中的更改

    1. 您使用Ext.create定义了列表,对此不确定。我变了 它
    2. onItemDisclosure,show是监听器所以,将它放在监听器中
    3. 使用storeId将列表与store: 'CurrencyStore'而不是store: Ext.create('DebtManager.store.Currency')
    4. 等商店联系起来
    5. 不要使用预定义的xtype作为自定义视图,所以我改变了 xtype: 'panel'xtype: 'currencyListPanel'
    6. 更新了TabPanel

      Ext.define('DebtManager.view.CurrenciesListPanel', {
          extend: 'Ext.tab.Panel',
          xtype: 'currencyListPanel',
      
          requires: [
              'DebtManager.store.Currency',       
          ],
      
          config: {
              title: 'Currencies',
              iconCls: 'action',  
      
              layout: 'fit',
      
              items: [
                  {
                      docked: 'top',      
                      xtype: 'titlebar',
                      title: 'Currencies tab',
                      items: [
                          { xtype: 'spacer' },
                          {
                              id: 'newNoteButton',
                              text: 'Add',
                              ui: 'action',
      
                              handler: function () {                          
                                  console.log('Add currency');
                                  var editorPanel = Ext.create('DebtManager.view.CurrencyEditorPanel');
                                  var currency = Ext.create( 'DebtManager.model.Currency', {title: 'xxx', dollarExchangeRate: '100'} );                           
                                  editorPanel.setRecord( currency );                          
                                  Ext.Viewport.setActiveItem( editorPanel, {type: 'slide', direction: 'left'});                           
                              }
                          }
                      ]               
                  },
                  {
                      xtype : 'list',
                      id: 'currenciesListPanelList',
                      title: 'Currencies list',
                      onItemDisclosure: true,
                      store: 'CurrencyStore', 
                      itemTpl: '<div class="list-item-title">{title}</div>' + '<div class="list-item-narrative">{dollarExchangeRate}</div>',
                      listeners : {
                          onItemDisclosure: function (record) {
                              // Edit item
                              console.log('Edit item');
                          },
      
                          show: function (list, opts) {
                              console.log('List Shown: ' + list);
                          }                    
                      }
      
                  }
              ],        
      
          },
      
      });
      

      附加说明

      itemIdgetComponent()一起使用idgetCmp()使用div.menu { background-color: yellow; display: table-cell; vertical-align: middle; text-align: center; height: 120px;}

      了解更多See This Screencast

      最后,以下是Sencha fiddle

      中的完整代码