Ext结合DWR的关键代码(运行成功的DWRProxy)
生活随笔
收集整理的這篇文章主要介紹了
Ext结合DWR的关键代码(运行成功的DWRProxy)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關鍵代碼如下:
Store為:
Store為:
- var?ds?=?new?Ext.data.Store({
- ????proxy:?new?Ext.data.DWRProxy({
- ????????callback:?Folder.getMessageList,
- ?????????params:?{
- ?????????????start:?'0',
- ?????????????limit:?PAGE_SIZE
- ?????????}
- ?????}),
- ????
- ????//????proxy:?new?Ext.data.MemoryProxy(messageList),
- ????
- ????reader:?new?Ext.data.ObjectReader({
- ????????root:?'messages',
- ????????totalProperty:?'total'//用來讓Ext的PagingToolbar組件分析總頁數
- ????},?[{
- ????????name:?'messageId'
- ????},?{
- ????????name:?'from'
- ????},?{
- ????????name:?'subject'
- ????},?{
- ????????name:?'sendTime'
- ????},?{
- ????????name:?'contentText'
- ????}])
- });
- //在進行DWR請求之前,默認在請求參數中添加當前文件夾ID。這樣DWR請求中包括的請求參數為:folderId,start,limit
- ds.on('beforeload',?function(){
- ????Ext.apply(this.baseParams,?{
- ????????folderId:?currentFolderId
- ????});
- });
- ds.load({
- ????????params:?{
- ????????????start:?currentStart,
- ????????????limit:?currentLimit
- ????????}
- ????});//指定起始位置以及個數
- Ext.data.DWRProxy?=?function(config){
- ??????Ext.data.DWRProxy.superclass.constructor.call(this);
- ??????Ext.apply(this,?config?||
- ??????{});
- ??};
- ??
- ??7Ext.extend(Ext.data.DWRProxy,?Ext.data.DataProxy,?{
- ??????//DWR請求
- ??????load:?function(params,?reader,?callback,?scope,?arg){
- ?????????currentStart?=?params.start;//保存當前頁記錄起始位置
- ?????????currentLimit?=?params.limit;//保存當前頁記錄的個數
- ?????????document.dwr?=?{
- ?????????????params:?params,
- ?????????????reader:?reader,
- ?????????????callback:?callback,
- ?????????????scope:?scope,
- ?????????????arg:?arg
- ?????????};
- ?????????//處理請求參數,將各個請求參數轉換到Array中
- ?????????var?callParams?=?new?Array();
- ?????????callParams.push(params.folderId);//當前文件夾ID
- ?????????callParams.push(params.start);//請求的起始位置
- ?????????callParams.push(params.limit);//請求的個數
- ?????????if?(this.params?!==?undefined?&&?this.params?!==?null)?{
- ?????????????this.callback.call(this,?callParams,?this.loadResponse);//DWR請求
- ?????????}
- ?????????else?{
- ?????????????this.callback.call(this,?this.loadResponse);
- ?????????}
- ?????},
- ?????//處理DWR返回
- ?????loadResponse:?function(response){
- ?????????var?dwr?=?document.dwr;
- ?????????try?{
- ?????????????//IE?throws?an?exception?'Error:?TypeError,?Object?does?not?support?this?operation'???
- ?????????????//so?use?trycatch?to?suppress?this?exception???
- ?????????????delete?document.dwr;//ie不支持delete
- ?????????}?
- ?????????catch?(e)?{
- ?????????}
- ?????????var?result;
- ?????????try?{
- ?????????????result?=?dwr.reader.read(response);//讀取請求返回的json
- ?????????}?
- ?????????catch?(e)?{
- ?????????????//this.fireEvent("loadexception",this,dwr,response,e);???
- ?????????????dwr.callback.call(dwr.scope,?null,?dwr.arg,?false);
- ?????????????return;
- ?????????}
- ????????dwr.callback.call(dwr.scope,?result,?dwr.arg,?true);
- ?????},
- ?????failure:?function(errorString,?exception){
- ?????????console.log("DWR?"?+?exception);
- ????},
- ?????update:?function(params,?records){
- ?????}
- ?});
- ?//自定義一個用于處理返回消息列表的Reader
- ?Ext.data.ObjectReader?=?function(meta,?recordType){
- ?????Ext.data.ObjectReader.superclass.constructor.call(this,?meta,?recordType);
- ?};
- ?Ext.extend(Ext.data.ObjectReader,?Ext.data.DataReader,?{
- ?????//處理DWR返回
- ?????read:?function(response){
- ?????????var?responseDecode?=?Ext.util.JSON.decode(response);//注意,由java的json-lib生成的json串需要解碼一下
- ?????????var?data?=?responseDecode.messages;
- ?????????var?sid?=?this.meta???this.meta.id?:?null;
- ?????????var?recordType?=?this.recordType,?fields?=?recordType.prototype.fields;
- ????????var?records?=?[];
- ?????????var?root?=?data;
- ????????for?(var?i?=?0;?i?<?root.length;?i++)?{
- ?????????????var?obj?=?root[i];
- ?????????????var?values?=?{};
- ?????????????var?id?=?obj[sid];
- ?????????????for?(var?j?=?0,?jlen?=?fields.length;?j?<?jlen;?j++)?{
- ?????????????????var?f?=?fields.items[j];
- ?????????????????var?k?=?(f.mapping?!==?undefined?&&?f.mapping?!==?null)???f.mapping?:?f.name;
- ?????????????????var?v?=?null;
- ????????????????var?idx?=?k.indexOf(".");
- ?????????????????if?(idx?==?-1)?{
- ?????????????????????v?=?obj[k]?!==?undefined???obj[k]?:?f.defaultValue;
- ?????????????????}
- ?????????????????else?{
- ?????????????????????var?k1?=?k.substr(0,?idx);
- ?????????????????????var?k2?=?k.substr(idx?+?1);
- ?????????????????????if?(obj[k1]?==?undefined)?{
- ?????????????????????????v?=?f.defaultValue;
- ?????????????????????}
- ?????????????????????else?{
- ?????????????????????????var?obj2?=?obj[k1];
- ?????????????????????????v?=?obj2[k2]?!==?undefined???obj2[k2]?:?f.defaultValue;
- ????????????????????}
- ?????????????????}
- ?????????????????v?=?f.convert(v);
- ?????????????????values[f.name]?=?v;
- ?????????????}
- ?????????????var?record?=?new?recordType(values,?id);
- ????????????records[records.length]?=?record;
- ????????}
- ????????return?{
- ????????????records:?records,//返回的消息列表記錄
- ????????????totalRecords:?responseDecode.total//總個數
- ????????};
- ????}
- });
轉載于:https://www.cnblogs.com/China-Dragon/archive/2010/05/07/1730156.html
總結
以上是生活随笔為你收集整理的Ext结合DWR的关键代码(运行成功的DWRProxy)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《清夜琴兴》第十句是什么
- 下一篇: 强烈推荐:240多个jQuery插件