2013-03-04 62 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()之后,您应该能够在商店中看到您的新商品。

相关问题