将Ajax respose数据存储在Sencha touch中

问题描述:

如何添加或保存从ajax请求获取或存储数据以存储或建模sencha touch 2 我有控制器,存储和模型。 Ext.Ajax.request();由控制器进行调用,当它成功我想移动的数据以JSON格式存储将Ajax respose数据存储在Sencha touch中

Ext.define('Myapp.controller.HomeController', { 
    extend: 'Ext.app.Controller', 

    config: { 
     control: { 

      "#homepage_id": { 
       show: 'onHomepage_idShow' 
      } 
     } 

    }, 


    onHomepage_idShow: function (component, eOpts) { 

     var token = localStorage.getItem('Token'); //************************************** 
     console.log('test home', token); 

     var customHeaders = { 
      'Content-Type': 'application/json; charset=utf-8', 
      'ApiAuth': token 
     }; 

     this.callAjax(customHeaders); 
    }, 
    callAjax: function (headers) { 
     var customHeaders = headers; 

     Ext.Ajax.request({ 
      url: 'http://localhost:9098/Folder/json/Get', 
      params: Ext.util.JSON.encode({ 
       folderId: 0 
      }), 
      method: 'POST', 

      headers: customHeaders, 

      success: function (response) { 
       var decode_text = Ext.decode(response.responseText); 
       /*I want to add decode_text to a store from this contoller..*/ 

       //var storez = Ext.data.StoreManager.lookup('commomStore_id');//**************** 

       //this.getDataList().setStore(storez); 
       console.log(storez); 


       // process server response here 
      }, 
      failure: function (response, opts) { 
       Ext.Msg.alert('Error', 'Error while submitting the form'); 
       console.log(response.responseText); 
      }, 

      scope: this 
     }); 

我的商店:

Ext.define('Myapp.store.CommonStore', { 
    extend: 'Ext.data.Store', 

    requires: [ 
     'Myapp.model.AuthTokenmodel'], 

    config: { 
     autoLoad: true, 
     model: 'Myapp.model.AuthTokenmodel', 
     storeId: 'commonStote_id', 
     proxy: { 

      type: 'localstorage', 
      id: 'commomStore_id', 
      reader: { 
       type: 'json' 
      } 
     }, 
     fields: [{ 
      name: 'authtoken' 
     }] 
    } 
}); 

对于您必须分析你的反应,并创建Myapp.model.AuthTokenmodel的物品运然后使用add方法将这些对象添加到您的商店。

顺便说一句,如果你的反应是JSON格式,你应该像这样解析为JSON而不是文本:

var respObj = Ext.JSON.decode(response.responseText); 
console.log(respObj); 

然后使用respObj数据建立模型对象,并添加这些存储:

var storez = Ext.data.StoreManager.lookup('commomStore_id'); 
storez.add(Ext.create('Myapp.model.AuthTokenmodel', {authtoken : respObj.authToken})); 

Ext.getStore('commomStore_id').loadData(decode_text);