2014-03-06 32 views
0

嗨,大家需要您的幫助來加載本地存儲。將現有本地存儲數據加載到商店並將其綁定到sencha中的選定字段

我想從我的本地存儲自動加載現有記錄。

然後綁定它來存儲和選擇字段。

我想加載現有記錄,以便當我在本地存儲中添加新記錄時,它不會重寫現有的ID編輯記錄。

下面是我的一些代碼

app.js

Ext.application({ 
    name: 'IO', 

    requires: [ 
     'Ext.MessageBox' 
    ], 

    views: [ 
     'Main' 
    ], 

    stores: [ 
     'Files' 
    ], 

    icon: { 
     '57': 'resources/icons/Icon.png', 
     '72': 'resources/icons/Icon~ipad.png', 
     '114': 'resources/icons/[email protected]', 
     '144': 'resources/icons/[email protected]' 
    }, 

    isIconPrecomposed: true, 

    startupImage: { 
     '320x460': 'resources/startup/320x460.jpg', 
     '640x920': 'resources/startup/640x920.png', 
     '768x1004': 'resources/startup/768x1004.png', 
     '748x1024': 'resources/startup/748x1024.png', 
     '1536x2008': 'resources/startup/1536x2008.png', 
     '1496x2048': 'resources/startup/1496x2048.png' 
    }, 

    launch: function() { 
     // Destroy the #appLoadingIndicator element 
     Ext.fly('appLoadingIndicator').destroy(); 

     // Initialize the main view 
     Ext.Viewport.add(Ext.create('IO.view.Main')); 
    }, 

    onUpdated: function() { 
     Ext.Msg.confirm(
      "Application Update", 
      "This application has just successfully been updated to the latest version. Reload now?", 
      function(buttonId) { 
       if (buttonId === 'yes') { 
        window.location.reload(); 
       } 
      } 
     ); 
    } 
}); 

Files.js - 商店

Ext.define('IO.store.Files', { 
    extend: 'Ext.data.Store', 
    alias: 'store.Files', 

    requires: [ 
     'IO.model.File' 
    ], 

    config: { 
     model: 'IO.model.File', 
     storeId: 'filestore', 
     proxy: { 
      type: 'localstorage', 
      id: 'filestoreproxy' 
     }, 
       autoLoad: true 
    } 
}); 

File.js - 型號

Ext.define('IO.model.File',{ 
    extend: 'Ext.data.Model', 
    alias: 'model.File', 

     config: { 
      fields: [ 
       { 
        name: 'id', 
        type: 'int' 
       }, 
       { 
        name: 'name', 
        type: 'string' 
       } 
      ] 
    } 
}); 

NumberListIO - 查看

Ext.define('IO.view.NumberListIO', { 
    extend: 'Ext.field.Select', 
    xtype: 'numberiolist', 
    id: 'IOSelectField', 
    cls: 'IOListSelectOption', 
    config:{ 
     xtype: 'selectfield', 
     name: 'test', 
     options: [ 
      { 
       text: "--- Select List ---", 
       value: "" 
      } 
     ], 
     store: 'filestore', 
     displayField: 'name', 
     valueField: 'id'   
    } 
}); 
+0

我看不出你的問題。用您需要的邏輯擴展您的控制器並將商店附加到視圖。 –

+0

那麼目前我沒有控制器,但我的問題是,當我添加項目到商店它成功保存在本地存儲和一個選項被添加到我的選擇字段,但是當我重新加載我的瀏覽器時,選擇字段再次爲空儘管在我的瀏覽器本地存儲中,我之前添加的數據在那裏。然後,當我嘗試再次添加項目時,現有數據將被新項目覆蓋。 –

回答

0

我已經想通了。

我已經裝上的應用程序推出的本地存儲數據到存儲

這裏是我的代碼

// Initialize localstorage 
    var options = []; 
    var numbers = []; 

    for (i=0; sKey = window.localStorage.key(i); i++) { 
     if(sKey.indexOf("filestoreproxy-ID_") !== -1){ 

      tmp = JSON.parse(window.localStorage.getItem(sKey)); 

      var myFile = Ext.create('IO.model.File',{ 
       id: tmp.id, 
       name: tmp.name 
      }); 

      options.push(myFile); 

     }else if(sKey.indexOf("numberstoreproxy-ID_") !== -1){ 

      tmp = JSON.parse(window.localStorage.getItem(sKey)); 

      var myNumber = Ext.create('IO.model.Number',{ 
       id: tmp.id, 
       file_id: tmp.file_id, 
       value: tmp.value, 
       frequency: tmp.frequency 
      }); 

      numbers.push(myNumber); 
     } 
    } 

    Ext.getStore('filestore').load(this, options); 
    Ext.getStore('numberstore').load(this, numbers); 
相關問題