Sencha Touch2中数据的获取
生活随笔
收集整理的這篇文章主要介紹了
Sencha Touch2中数据的获取
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
根據(jù)Sencha Touch技術(shù)框架的特點(diǎn),前臺(tái)展示的絕大部分?jǐn)?shù)據(jù)都是通過ajax方式獲取,譬如列表數(shù)據(jù)的獲取、表單數(shù)據(jù)的獲取等等。列表數(shù)據(jù)的獲取,一般是通過store組件和list組件進(jìn)行結(jié)合;表單數(shù)據(jù)的獲取通常使用Ext.request方式獲取。
列表數(shù)據(jù)的獲取代碼清單:
//數(shù)據(jù)列表renderListData: function(){//定義變量和函數(shù)的作用范圍var _scope = this;var st = new Ext.data.Store({model: _scope.model, //store組件使用的Ext.data.Model//sorters: 'crtdt',autoLoad: true, //是否自動(dòng)加載數(shù)據(jù)proxy: {type: 'ajax',actionMethods: 'POST',url: _scope.url, //數(shù)據(jù)獲取的url地址extraParams: _scope.extraParams, //請(qǐng)求的額外參數(shù)startParam: 'start',limitParam: 'limit',reader: {type: 'json', //reader類型-默認(rèn)jsonReader,root: undefined, //reader root-默認(rèn)undefined}},listeners: {beforeload: function(st, oper){oper.start = _scope.start; //設(shè)置分頁(yè)起始記錄oper.limit = _scope.limit; //設(shè)置每頁(yè)顯示記錄數(shù)},load: function(st, records, successful){ if(successful){//獲取后臺(tái)返回的記錄總數(shù)屬性_scope.totalNum = st.getProxy().getReader().rawData.total;//分頁(yè)按鈕控制_scope.controlPageButtons();//回調(diào)外部函數(shù)-返回結(jié)果給外部函數(shù)if(_scope.callbackFn){_scope.callbackFn({listId:_scope.listId, total:_scope.totalNum});}} }}});//創(chuàng)建并返回list組件return new Ext.List({id: _scope.listId,//multiSelect: true,//simpleSelect: true,scroll: 'vertical',store: st, //store組件對(duì)象emptyText: '<div style="margin:2px;">'+_scope.emptyText+'</div>',loadingText: '正在獲取數(shù)據(jù)',itemTpl: _scope.itemTpl, //列表顯示的模板listeners: {itemtap: function(lt, idx, item, e){//點(diǎn)擊列表記錄時(shí)執(zhí)行的函數(shù)if(_scope.itemTapFn){var record = lt.getStore().getAt(idx);_scope.itemTapFn({listId:_scope.listId, record:record}, _scope.fnScope);}}}});}
通過Ext.request方式獲取數(shù)據(jù)的代碼清單:
//獲取數(shù)據(jù)fetchData: function(){var _scope = this;Ext.Ajax.request({method: 'POST',url: prefix + '/doc/letter/manager!fetchEditData.action',params: {'entity.id': _scope.entityId //傳遞給后臺(tái)的參數(shù)},success: function(response, opts){var objResp = Ext.decode(response.responseText);if(objResp.success){var objData = Ext.decode(objResp.result);//調(diào)用自定義的函數(shù)把數(shù)據(jù)設(shè)置到form中_scope.loadDataToForm(_scope, objData);}else{Ext.Msg.alert(objResp.result);}},failure: function(response, opts){Ext.Msg.alert('很抱歉,由于網(wǎng)絡(luò)原因獲取數(shù)據(jù)出錯(cuò)!');}});}
列表數(shù)據(jù)的獲取代碼清單:
//數(shù)據(jù)列表renderListData: function(){//定義變量和函數(shù)的作用范圍var _scope = this;var st = new Ext.data.Store({model: _scope.model, //store組件使用的Ext.data.Model//sorters: 'crtdt',autoLoad: true, //是否自動(dòng)加載數(shù)據(jù)proxy: {type: 'ajax',actionMethods: 'POST',url: _scope.url, //數(shù)據(jù)獲取的url地址extraParams: _scope.extraParams, //請(qǐng)求的額外參數(shù)startParam: 'start',limitParam: 'limit',reader: {type: 'json', //reader類型-默認(rèn)jsonReader,root: undefined, //reader root-默認(rèn)undefined}},listeners: {beforeload: function(st, oper){oper.start = _scope.start; //設(shè)置分頁(yè)起始記錄oper.limit = _scope.limit; //設(shè)置每頁(yè)顯示記錄數(shù)},load: function(st, records, successful){ if(successful){//獲取后臺(tái)返回的記錄總數(shù)屬性_scope.totalNum = st.getProxy().getReader().rawData.total;//分頁(yè)按鈕控制_scope.controlPageButtons();//回調(diào)外部函數(shù)-返回結(jié)果給外部函數(shù)if(_scope.callbackFn){_scope.callbackFn({listId:_scope.listId, total:_scope.totalNum});}} }}});//創(chuàng)建并返回list組件return new Ext.List({id: _scope.listId,//multiSelect: true,//simpleSelect: true,scroll: 'vertical',store: st, //store組件對(duì)象emptyText: '<div style="margin:2px;">'+_scope.emptyText+'</div>',loadingText: '正在獲取數(shù)據(jù)',itemTpl: _scope.itemTpl, //列表顯示的模板listeners: {itemtap: function(lt, idx, item, e){//點(diǎn)擊列表記錄時(shí)執(zhí)行的函數(shù)if(_scope.itemTapFn){var record = lt.getStore().getAt(idx);_scope.itemTapFn({listId:_scope.listId, record:record}, _scope.fnScope);}}}});}
通過Ext.request方式獲取數(shù)據(jù)的代碼清單:
//獲取數(shù)據(jù)fetchData: function(){var _scope = this;Ext.Ajax.request({method: 'POST',url: prefix + '/doc/letter/manager!fetchEditData.action',params: {'entity.id': _scope.entityId //傳遞給后臺(tái)的參數(shù)},success: function(response, opts){var objResp = Ext.decode(response.responseText);if(objResp.success){var objData = Ext.decode(objResp.result);//調(diào)用自定義的函數(shù)把數(shù)據(jù)設(shè)置到form中_scope.loadDataToForm(_scope, objData);}else{Ext.Msg.alert(objResp.result);}},failure: function(response, opts){Ext.Msg.alert('很抱歉,由于網(wǎng)絡(luò)原因獲取數(shù)據(jù)出錯(cuò)!');}});}
轉(zhuǎn)載于:https://www.cnblogs.com/fyq891014/archive/2012/06/02/4188866.html
總結(jié)
以上是生活随笔為你收集整理的Sencha Touch2中数据的获取的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 回头再见他
- 下一篇: javascirpt历史澄清误解基本概念