ArcGis Server ADF Toolbar与Map的客户端和服务端交互过程分析2
2、客户端如何发送信息到服务端?
当我们单击工具条上的一个按钮时,根据上面的分析,ToolbarMouseDown()被执行。
我们来看看ToolbarMouseDown()具体干啥:
function ToolbarMouseDown(toolbarName, toolbarItemName, buttonType, e)
{
.......
if (buttonType == "Tool") {
......
var clientAction = toolbar.items[toolbarItemName].clientAction;
if (clientAction != null)
{
var clientActions = "";
if (!toolbar.items[toolbarItemName].isClientActionCustom)
{
var buddies = toolbar.buddyControls;
if (buddies != null) {
for (var i = 0; i < buddies.length; i++)
{
var modeField = f.elements[buddies[i] + "_mode"];
if (modeField != null)
modeField.value = toolbarItemName;
var cursor = toolbar.items[toolbarItemName].cursor;
if (cursor != null)
clientActions = clientActions + clientAction + " ( '" +buddies[i] + "' , '" + toolbarItemName + "', " +toolbar.items[toolbarItemName].showLoading + ",'" + cursor + "'); ";
else
clientActions = clientActions + clientAction + " ( '" +buddies[i] + "' , '" + toolbarItemName + "', " +toolbar.items[toolbarItemName].showLoading + "); ";
}
}
}
else
{
clientActions = clientAction;
}
...
if (toolbar.items[toolbarItemName].preExecFunction != null)
clientActions += toolbar.items[toolbarItemName].preExecFunction;
// 调用ClientAction
var clientActionFunction = new Function(clientActions);
clientActionFunction.call(null);
//select this tool and unselect others
Toolbars[toolbarName].selectTool();
Toolbars[toolbarName].refreshGroup();
}
}
....
}
这段代码的简单意思就是说,如果ClientAction是我们自定义的,则直接执行。如果
不是呢,则构造如下形式的调用:
Function.Call("Map[XXX]('Map1','[ToolName]',showLoading,cursor");
MapXxx(..)其实是Map内部提供的JS函数(ESRI.ADF.UI.Map.js里边),总共有如下类型:
MapDragImage = ESRI.ADF.MapTools.MapDragImage = function(mapid, mode, showLoading, cursor);
MapDragRectangle = ESRI.ADF.MapTools.DragRectangle = function(mapid, mode, showLoading, cursor) ;
MapBox = MapDragBox = ESRI.ADF.MapTools.DragRectangle;
MapPoint = ESRI.ADF.MapTools.Point = function(mapid, mode, showLoading, cursor) ;
MapLine = ESRI.ADF.MapTools.Line = function(mapid, mode, showLoading, cursor, vectorToolbarState) ;
MapPolyline = ESRI.ADF.MapTools.Polyline = function(mapid, mode, showLoading, cursor, vectorToolbarState) ;
MapPolygon = ESRI.ADF.MapTools.Polygon = function(mapid, mode, showLoading, cursor, vectorToolbarState) ;
MapDragCircle = MapCircle = ESRI.ADF.MapTools.Circle = function(mapid, mode, showLoading, cursor, vectorToolbarState) ;
MapDragOval = MapOval = ESRI.ADF.MapTools.Oval = function(mapid, mode, showLoading, cursor, vectorToolbarState) ;
现在我们以MapDragRectangle()为来自来分析:
MapDragRectangle = ESRI.ADF.MapTools.DragRectangle = function(mapid, mode, showLoading, cursor)
{
var map = $find(mapid);
if(!map) { map=Pages[mapid]; }
if(ESRI.ADF.UI.Map.isInstanceOfType(map))
{
if(mode==='MapZoomIn')
{
map.set_mouseMode(ESRI.ADF.UI.MouseMode.ZoomIn);
}
else if(mode==='MapZoomOut')
{
map.set_mouseMode(ESRI.ADF.UI.MouseMode.ZoomOut);
}
else
{
var onComplete = Function.createDelegate(map, function(geom) {
var geomString = geom.get_xmin()+':'+geom.get_ymin()+'|'+geom.get_xmax()+':'+geom.get_ymax();
this.doCallback('EventArg=DragRectangle&coords='+geomString+'&'+mapid+'_mode='+mode,this);
});
map.getGeometry(ESRI.ADF.Graphics.ShapeType.Envelope,onComplete,function(){map.__activeToolMode=null;},ESRI.ADF.MapTools.lineColor,ESRI.ADF.MapTools.fillColor,cursor,true);
map.__activeToolMode = mode;
}
..
}
}
...
};
当Tool Name是MapZoomIn和MapZoomOut时,Map内部提供支持。否则就执行一些操作之
后,然后把操作信息通过doCallback()发送到Server端。所谓执行一些操作,实际上
就是画图,通过调用map.getGeometry(...,onComplete,...)来执行,当执行完成之后,
onComplete被执行。
var onComplete = Function.createDelegate(map, function(geom)
{
var geomString = geom.get_xmin()+':'+geom.get_ymin()+'|'+geom.get_xmax()+':'+geom.get_ymax();
this.doCallback('EventArg=DragRectangle&coords='+geomString+'&'+mapid+'_mode='+mode,this);
});
执行操作完成之后,map会构造图形的geom实例,并作为onComplete()的参数。通过上
面的代码我们状况,这个时候只是简单执行map.doCallback()把信息发送回到Server。
map.doCallback()用到一个重要的内部变量callbackFunctionString,这个变量的值由
Server端ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl控件生成,看看它的生成代码:
protected internal virtual string CallbackFunctionString
{
get
{
if (this.RequiresPartialPostback)
{
this.callbackFunctionString =string.Format("__esriDoPostBack('{0}','{1}', argument,ESRI.ADF.System.ProcessMSAjaxCallbackResult, context)", this.UniqueID,this.ClientID);
}
else
{
//生成语句:WebForm_DoCallback('__Page',argument,ESRI.ADF.System.processCallbackResult,context,postBackError,true);
this.callbackFunctionString =this.Page.ClientScript.GetCallbackEventReference(this, "argument","ESRI.ADF.System.processCallbackResult", "context", "postBackError",true);
}
return this.callbackFunctionString;
}
}
RequiresPartialPostback在什么情况为TRUE呢?就是当你的Page中存在一个
ScriptManager时,这个值为TRUE,执行部分Postback。如果没有ScriptManager,
将执行Callback。通过上面代码我知道,如果是postback,则执行:
__
__esriDoPostBack(this.UniqueID,this.ClientID, argument, ESRI.ADF.System.ProcessMSAjaxCallbackResult, context)
如果是callback,则执行:
WebForm_DoCallback('__Page',argument,ESRI.ADF.System.processCallbackResult,context,postBackError,true);
不管是Postback还是Callback,信息发送到服务端之后,将由服务端进行处理,然后
再回传信息到客户端,由CallbackFunctionString里边定义的JS函数处理.如果是postback,
则由ESRI.ADF.System.ProcessMSAjaxCallbackResult()处理,否则由ESRI.ADF.System.processCallbackResult()处理。