2014-10-17 73 views
0

嗨,我想在我的应用程序中使用JavaScript会话存储在sencha触摸模型,在互联网上长时间搜索后,我没有得到幸运,请帮助,如果可以,谢谢。 这是我的代码到目前为止。
我的控制器的onStorage功能工作,getStorage usession.load加载失败,那是我又立刻陷入
Sencha Touch sessionstorage,保存和检索

Ext.define('SideMenu.controller.Menu', { 
extend: 'Ext.app.Controller', 
currView:'home', 
requires: ['SideMenu.model.Mymdl'], 
config: { 
     refs: { 
       btn_localstore:'#btn_localstore', 
       btn_getlocalstore:'#btn_getlocalstore'      
       } 
     control: { 
       btn_localstore:{ 
       tap: 'onStorage' 
       }, 
       btn_getlocalstore:{ 
       tap: 'getStorage' 
       },      
       }, 
     onStorage:function(){ 
         //create model and store data 
         var myMod = Ext.create('SideMenu.model.Mymdl',{ 
         brandName:'Nike Jordan', 
         brandCat:'Sneakers'   
         }); 
         myMod.save({ 
          success:function(res){ 
          console.log('saved to model : '+res.get('brandName')); 
           } 
          });    
       }, 
      getStorage:function(){ 
       var usession = Ext.ModelMgr.getModel('SideMenu.model.Mymdl'); 
       console.log('model is :'+usession); 

       usession.load(0, { 
          scope: this, 
          success: function(model, opp) { 
          console.log('passed '+model.get('brandCat')); 
           }, 
       failure: function(record, operation) { 
       console.log('failed : '+operation); 
       // Ext.Viewport.setMasked(false); 
       //==================================================== 
       // alert('could not get session details'); 
       //==================================================== 
        } 
        }); 

    } 
} 


我的模型

Ext.define('SideMenu.model.Mymdl', { 
extend : 'Ext.data.Model', 
xtype : 'Mymdl', 
requires:['Ext.data.proxy.SessionStorage'], 
id : 'Mymdl', 
config: { 
    fields: [ 
     'brandName', 
     'brandCat'  
    ] 
    , 

    proxy : { 
     type: 'localstorage', 
     id : 'mymdl' 
    } 
} 
}); 


我app.js我排除了其他不需要的东西dts

models: ['Mymdl'], 
views: ['Main', 'Home'], 
controllers: ['Menu'], 

launch:function() 
{ 
    Ext.create('SideMenu.model.Mymdl'); 
} 

你的回答将不胜感激,谢谢

回答

1

您将需要调用使用您想从本地存储检索模型数据的ID模型加载方法。

您可以从模型保存回调函数,当你加载模型

var modelId; 
myMod.save({success:function(res){ 
       modelId = res.getId(); 
       console.log('saved to model : '+res.get('brandName')); 
      } 
      }); 

然后使用这个号码,获得ID:

SideMenu.model.Mymdl.load(modelId, function(record) { 
    console.log('Brand: ' + record.get('brandName')); 
} 

当您保存您可以直接设置id的值模型。这样可以避免您必须在每次保存时检索并保存自动生成的ID。

+0

杰夫马丁,你刚刚救了我的命人,非常感谢,我为此挣扎了两天......让一个Sencha无法帮助我的人,不幸的是我无法提升我低声誉的CS。 .bt thnks bru,ura genius man – 2014-10-20 06:48:28

+0

还有一件事,当我保存模型时,如何设置id值。 – 2014-10-20 06:59:27

+0

模型ID属性是自动创建的。您可以使用model.set('id,[value])来设置值。 – 2014-10-21 10:45:40