`
yiminghe
  • 浏览: 1475199 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

简析 DWRProxy

阅读更多
//(代码系转载:)
Ext.namespace("Ext.ux.data");

/**
 * @class Ext.ux.data.DWRProxy
 * @extends Ext.data.DataProxy
 * @author loeppky
 * An implementation of Ext.data.DataProxy that uses DWR to make a remote call.
 * @constructor
 * @param {Object} config A configuration object.
 */
Ext.ux.data.DWRProxy = function(config) {
    Ext.apply(this, config);
    // necessary since the superclass doesn't call apply
    Ext.ux.data.DWRProxy.superclass.constructor.call(this);
};

Ext.extend(Ext.ux.data.DWRProxy, Ext.data.DataProxy, {

    /**
	 * @cfg {Function} dwrFunction The DWR function for this proxy to call during load.
	 * Must be set before calling load.
	 */
    dwrFunction: null,

    /**
	 * @cfg {String} loadArgsKey Defines where in the params object passed to the load method
	 * that this class should look for arguments to pass to the "dwrFunction".
	 * The order of arguments passed to a DWR function matters.
	 * Must be set before calling load.
	 * See the explanation of the "params" parameter for the load function for further explanation.
	 */
    loadArgsKey: 'dwrFunctionArgs',

    /**
	 * Load data from the configured "dwrFunction",
	 * read the data object into a block of Ext.data.Records using the passed {@link Ext.data.DataReader} implementation,
	 * and process that block using the passed callback.
	 * @param {Object} params An object containing properties which are to be used for the request to the remote server.
	 * Params is an Object, but the "DWR function" needs to be called with arguments in order.
	 * To ensure that one's arguments are passed to their DWR function correctly, a user must either:
	 * 1. call or know that the load method was called explictly where the "params" argument's properties were added in the order expected by DWR OR
	 * 2. listen to the "beforeload" event and add a property to params defined by "loadArgsKey" that is an array of the arguments to pass on to DWR.
	 * If there is no property as defined by "loadArgsKey" within "params", then the whole "params" object will be used as the "loadArgs".
	 * If there is a property as defined by "loadArgsKey" within "params", then this property will be used as the "loagArgs".
	 * The "loadArgs" are iterated over to build up the list of arguments to pass to the "dwrFunction".
	 * @param {Ext.data.DataReader} reader The Reader object which converts the data object into a block of Ext.data.Records.
	 * @param {Function} callback The function into which to pass the block of Ext.data.Records.
	 * The function must be passed <ul>
	 * <li>The Record block object</li>
	 * <li>The "arg" argument from the load function</li>
	 * <li>A boolean success indicator</li>
	 * </ul>
	 * @param {Object} scope The scope in which to call the callback
	 * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
	 */
    load: function(params, reader, loadCallback, scope, arg) {
        var dataProxy = this;
        if (dataProxy.fireEvent("beforeload", dataProxy, params) !== false) {
            var loadArgs = params[this.loadArgsKey] || params;
            // the Array or Object to build up the "dwrFunctionArgs"
            var dwrFunctionArgs = [];
            // the arguments that will be passed to the dwrFunction
            //dwr 用户参数
            //可以为  json 字符串 ,服务器再分析,或直接 字符串数组 ,利用dwr 处理参数对应
            if (loadArgs instanceof Array) {
                // Note: can't do a foreach loop over arrays because Ext added the "remove" method to Array's prototype.
                // This "remove" method gets added as an argument unless we explictly use numeric indexes.



                for (var i = 0; i < loadArgs.length; i++) {
                    dwrFunctionArgs.push(loadArgs[i]);
                }
            } else {
                // loadArgs should be an Object
                for (var loadArgName in loadArgs) {
                    dwrFunctionArgs.push(loadArgs[loadArgName]);
                }
            }

            //dwr 系统参数格式
            dwrFunctionArgs.push({
                callback: function(response) {
                    // call readRecords verses read because read will attempt to decode the JSON,
                    // but as this point DWR has already decoded the JSON.
                    //读取记录 ,请服务器返回json格式 ,这里reader为ext自带的 json reader
                    var records = reader.readRecords(response);
                    dataProxy.fireEvent("load", dataProxy, response, loadCallback);
                    loadCallback.call(scope, records, arg, true);
                },
                exceptionHandler: function(message, exception) {
                    // the event is supposed to pass the response, but since DWR doesn't provide that to us, we pass the message.
                    dataProxy.fireEvent("loadexception", dataProxy, message, loadCallback, exception);
                    loadCallback.call(scope, null, arg, false);
                }
            });
            this.dwrFunction.apply(Object, dwrFunctionArgs);
            // the scope for calling the dwrFunction doesn't matter, so we simply set it to Object.
        } else {
            // the beforeload event was vetoed
            callback.call(scope || this, null, arg, false);
        }
    }
});

 

store 流程

 

store.load {
    if(proxy) {
            proxy.load  {
                  callback:{
                     调用 reader 读取 返回数据 得到 records
                     调用store 的一个 回调函数,参数 records
                   }
            }
    }
}

store的回调函数 (records) {
          更新 界面,刷新数据
}
 

 


总结


  • Store:最主要的对外交互接口,内部保存了一个数据集。
  • Record:数据集里面的每一条记录。
  • DataField: 定义了记录每个字段的schema。
  • Proxy: 获取数据的代理。
  • DataReader: 把代理读取的数据读入数据集。

Store的实现

  • 内部用一个Ext .util.MixedCollection(既能够作为map进行索引,又能够作为list进行索引,并且更新数据时会触发事件,并且有一些其它功能的数据结构)作为数据集存放record。store实例的data域指向这个数据集。
  • 通过proxy加载不同来源的数据,如http remote,memory data等等。
  • 由于proxy加载过来的数据是异构的,所以通过不同DataReader的实现类来读入数据成为record。
  • 当调用load方法时候,就会委托proxy去加载数据。
  • 在data数据集中数据的数据,添加,修改,删除的时候触发事件。


Store数据加载流程简介

  1. 实例化一个Proxy的实现proxy。
  2. 实例化一个DataReader的实现reader。
  3. 实例化一个Store对象store。设置store的proxy与reader。
  4. 监听store的load事件或者datachange事件。
  5. 调用store.load()
  6. store.load会调用proxy的load方法来加载数据(各种DataSource)。并且把reader作为参数传入。
  7. proxy的load方法加载完数据之后,调用传入的reader的read方法,并且把数据作为参数传入。
  8. reader的read方法把异构数据读入成record数据集。
  9. proxy把reader.read方法生成的record数据集作为参数,回调store。
  10. store把数据集添加到或者替换原来的数据集。
  11. 触发load事件,触发datachange事件。

 

分享到:
评论

相关推荐

    Ext的DWRProxy应用事例

    "Ext的DWRProxy应用事例"这个主题聚焦于如何利用Direct Web Remoting (DWR) 和 ExtJS 的DWRProxy组件进行异步数据通信。 DWR是一种JavaScript框架,它允许Web页面与Java后台服务器进行实时、安全、跨域的数据交换。...

    dwrproxy.js

    《DWRProxy.js:在ExtJS中的应用与实践》 DWRProxy.js是Direct Web Remoting (DWR)框架中的一个重要组成部分,它在JavaScript和Java之间建立起桥梁,使得Web应用程序可以方便地进行异步通信。DWR允许前端JavaScript...

    DWRproxy

    DWRproxy,全称Direct Web Remoting Proxy,是EXT框架中的一个重要组成部分,主要用来实现浏览器与服务器之间的异步通信。EXT是一个强大的JavaScript库,用于构建富客户端应用,而DWRproxy则是EXT与服务器端交互的...

    ext中dwrproxy与json处理数据技术

    ExtJs中使用dwrproxy,和json来处理从数据库里查询出来的数据 其中dwrproxy还支持分页(分页功能没做),将war包下载下来后,直接放在tomcat里,然后启动tomcat就可以了 数据库方面,该项目里用的是mysql,数据文件在...

    ExtJs DWR扩展 DWRProxy、DWRTreeLoader、DWRGridProxy

    1. **DWRProxy**: DWRProxy是ExtJs中的一个类,它作为DWR和ExtJs之间的桥梁,使得ExtJs可以利用DWR的远程调用功能。通过配置DWRProxy,开发者可以定义哪些服务器端的方法可供JavaScript调用,并控制这些调用的参数和...

    DWRProxy的运用实例,Ext,Dwr,Spring的完美结合

    这个例子中有登录,有...表格中的数据是通过DWRProxy加载共分3中形式,用以下3中解析器来解析的: DWRJsonReader DWRArrayReader DWRXmlReader 数据完全由JAVA方法返回,由DWR动态调用,利用了Spring作为Bean的容器。

    dwrProxy.js

    dwrProxy.js

    dwrproxy dwrtreeloader

    标题中的"dwrtproxy"和"dwrproxy.js"指的是DWR中的一个关键组件——DWR Proxy,而"dwrtreeloader"和"DWRTreeLoader.js"则涉及ExtJS中的TreePanel与DWR的集成,用于加载和展示树形结构数据。 **1. DWR(Direct Web ...

    EXTJS 3 整合DWR (DWRProxy、DWRTreeLoader、DWRGridProxy )

    EXTJS 3 整合DWR (DWRProxy、DWRTreeLoader、DWRGridProxy) 是一个在EXTJS 3.0版本中实现的重要技术整合,它将DWR(Direct Web Remoting)的强大功能引入到EXTJS的前端框架中,以实现更高效的数据交互。EXTJS是一个...

    Ext扩展dwrproxy

    Ext扩展dwrproxyExt扩展dwrproxyExt扩展dwrproxyExt扩展dwrproxyExt扩展dwrproxyExt扩展dwrproxyExt扩展dwrproxyExt扩展dwrproxyExt扩展dwrproxy

    dwrproxy.js,DWRTreeLoader.js,PagingDWRProxy.js

    本篇文章将深入探讨在Ext框架下,与DWR(Direct Web Remoting)集成所使用的三个关键JavaScript文件:`dwrproxy.js`、`DWRTreeLoader.js`和`PagingDWRProxy.js`。这些文件对于实现动态数据交互和页面组件的高效加载...

    Ext2.2.GridPanel分页处理+dwrproxy(js对象和json两种数据)

    在这个例子中,我们创建了一个使用DWRProxy的Store,DWRProxy允许我们在后台Java方法和前端JavaScript之间进行透明的数据交换。DWR方法`YourDWRMethod`应该接受分页参数(start和limit),并返回包含数据和总记录数...

    ext js配合dwr在java中的用法

    dwrproxy.js 博文链接:https://cicada-it.iteye.com/blog/102949

    DwrProxy.js

    EXT2未加入对DWR的支持,此文件是对其的扩展进而实现DWR的支持

    DWRProxy.js,DWRTreeLoader.js,PagingDWRProxy.js

    用EXT和DWR结合起来做工程时,在数据传输时会有点麻烦,这里提供的3个JS文件分别实现了DWR的数据代理,分页查询处理,动态树加载。并且实现了Ext.data.DWRArrayReader Ext.data.DWRXmlReader ...

    可用的dwrproxy.js,参考我的文章EXT+DWR分页

    EXT+DWR分页 ,前人基础修改,绝对能用 有问题的大家相互交流, 具体使用请参考我的文章EXT+DWR分页

    extjs兼容dwr的代理和DWRTreeLoader

    本主题将深入探讨如何在ExtJS中使用DWR的代理(DWRProxy)以及树结构加载器(DWRTreeLoader),以便于在ExtJS组件,特别是树形控件中有效地利用DWR的功能。 首先,让我们了解DWRProxy。DWRProxy是ExtJS中用于与DWR...

    ext + dwr proxy

    具体来说,`dwrproxy.js`可能是这个示例的核心文件,它实现了EXT和DWR之间的代理配置和通信。 在EXT中,DWR通常被用来作为后端数据源,为EXT的组件如Grid、Tree或Form提供数据。`dwrproxy.js`文件可能包含了以下...

Global site tag (gtag.js) - Google Analytics