2013-03-04 60 views
0

我已經創建了一個帶有2個文本字段和按鈕的表單。我想在用戶單擊按鈕時將文本字段值放入列表視圖中。所以我爲它創建了一個Model和Store。我的列表也在主視圖中查看代碼。但我在添加列表視圖中的記錄時遇到問題。它僅添加List中的當前記錄。它不會將存儲中的記錄呈現到列表中,並且我單擊刷新按鈕,但列表顯示爲空。這意味着數據存儲不正確。你能告訴我我的代碼中做了什麼錯嗎?商店不在列表中使用sencha

型號

Ext.define("panellist.model.User",{ 
     extend:'Ext.data.Model', 
     config: 
     { 

     fields: 
     [ 
      {name:'name',type:'string'}, 
      {name:'add',type:'string'} 
     ] 

     } 

    }); 

商店

Ext.define("panellist.store.Company", 
{ 
    extend:'Ext.data.Store', 
    config: 
{ 
     model:'panellist.model.User', 
     autoload:true, 
    proxy: 
    { 
     type:'localstorage', 
     id:'mypanellist' 
    } 
} 


}); 

Main.js

Ext.define('panellist.view.Main', 
    { 
     extend: 'Ext.Container', 
     xtype: 'main', 
     id:'mainview', 
    requires: 
    [ 
     'Ext.TitleBar', 
     'Ext.Panel', 
     'Ext.List', 
     'Ext.data.proxy.LocalStorage' 
    ], 

    config: 
    { 
    items: 
    [ 
     { 
     xtype:'titlebar', 
     title:'welcome to sencha touch', 
     docked:'top', 

     }, 
     { 

     xtype:'list', 
     width:'100%', 
     height:'100%', 
     itemTpl:'{name},{add}', 
     store:"Company" 

     }, 
     { 
     xtype:'titlebar', 
     docked:'bottom', 

     items: 
     [ 
     { 
      xtype:'textfield', 
      lablel:'enter the name', 
      value:'enter the name', 
      id:'t1', 
      name:'txtname' 
     }, 
     { 
      xtype:'spacer', 
     }, 
     { 
      xtype:'textfield', 
      value:'enter the address', 
      name:'txtadd', 
      id:'t2', 
     }, 
     { 
      xtype:'spacer', 
     }, 

     { 
      xtype:'button', 
      text:'Add', 
      action:'btnadd' 
     } 

     ] 
    } 
    ]} 

}); 

MyController.js

var mdl=Ext.create('panellist.model.User'); 

Ext.define("panellist.controller.MyController", 
{ 
      extend:'Ext.app.Controller', 
config: 
{ 
     refs: 
     { 
      // pass the id of the view 
      nav:'#mainview' 
     } 


}, 
init:function() 
{ 
       console.log("init is callled"); 
this.control({ 
'button[action=btnadd]': 
{ 
       tap:'insertrecords' 
} 

}); 
}, 

launch:function() 
{ 
      console.log("launch is callled"); 
}, 
insertrecords:function() 
{ 
// fetch the records from field 

var n=Ext.getCmp('t1').getValue(); 
var a=Ext.getCmp('t2').getValue(); 
// first store this values in model 

mdl.set("name",n); 
mdl.set("add",a); 
// add model into the store 
var store1=Ext.getStore("Company"); 

store1.add(mdl); 
store1.sync(); 

console.log('records is added'); 


console.log(n); 

} 
}); 

回答

0

我看到你的代碼中的幾個可疑區域。

冷杉你應該給你的商店storeId。這將使您可以在控制器中找到Ext.getStore()

您的Main.jsrequire您的店鋪的完整路徑,因爲您在其中一個項目中引用它。

requires: 
    [ 
     'Ext.TitleBar', 
     'Ext.Panel', 
     'Ext.List', 
     'Ext.data.proxy.LocalStorage', 
     'panellist.store.Company'  // <~ added 
    ], 

最後,如上所述,在您的控制器中使用新的storeId來獲得商店。

var store1 = Ext.getStore('newStoreId') 

給那些試試看,告訴我你想出了什麼。

當您運行代碼時,請在var store1 = Ext.getStore('..')處設置斷點以確保您獲得商店,並在添加元素時逐步完成。在sync()之後,您應該能夠在商店中看到您的新商品。

相關問題