function identificationTool(action,qryUE,queryTaskUE) { alert(action); if(action =="go") { dojo.connect(map, "onClick", function(evt) { console.log("Identification: click"); //onClick event returns the evt point where the user clicked on the map. //This is contains the mapPoint (esri.geometry.point) and the screenPoint (pixel xy where the user clicked). //set query geometry = to evt.mapPoint Geometry qryUE.geometry = evt.mapPoint; //Execute task and call showResults on completion queryTaskUE.execute(qryUE, showResults); }); } else { dojo.connect(map, "onClick", function(evt) { // Here we should stop the execution of the queryTaskUE map.infoWindow.hide(); }); } }
Solved! Go to Solution.
function init() { ... $(document).ready(function() { toolAction(map,qryUE,queryTaskUE); }); } function toolAction(map,qryUE,queryTaskUE) { ... $("#identify_spot").bind("click", function(event) { // If it's the same tool if( getActiveTool() == "#identify_spot" ) { setActiveTool(); // Clear all the graphics, but not the infoTemplate and I don't know why. map.graphics.clear(); dojo.disconnect(getDojoClick()); } // If it's the not same tool else { setActiveTool("#identify_spot"); setDojoClick(dojo.connect(map, "onClick", function(evt) { identificationTool(qryUE,queryTaskUE,evt); console.log("identification click"); }) ); } }); } var thisTool; var dojoAction; function setActiveTool(tool) { if(tool != null) { thisTool = tool; $(thisTool).addClass("active_tool"); } else { $(thisTool).removeClass("active_tool"); thisTool = ""; } console.log(thisTool+" class=active_tool") } function getActiveTool() { return thisTool; } function setDojoClick(action) { if( action != null ) { dojoAction = action; } else { dojoAction = "vide"; } } function getDojoClick() { console.log("kill"); return dojoAction; }
function identificationTool(action,qryUE,queryTaskUE) { console.log("identification? "+action) if(action == true ) { dojo.connect(map, "onClick", function(evt) { console.log("Identification: click"); qryUE.geometry = evt.mapPoint; queryDeferred = queryTaskUE.execute(qryUE); queryDeferred.then(showResults,showError); }); } if(action == false) { cancelExecute(); } } function cancelExecute() { console.log("identification queryDeferred("+queryDeferred+") to stop = "+queryDeferred.fired) if(queryDeferred && (queryDeferred.fired > 0)) { console.log("Identification: stop"); queryDeferred.cancel(); } }
var def = queryTask.execute(query, function (featureSet) { alert("FeatureSet returned"); }, function (err) { alert("Error...but can't get at the message/reason that you expected to see, unfortunately.\n\ndef.canceled (sic) = " + def.canceled); } ); def.cancel("A reason for cancelling can be provided here",/*not strict*/false);
Looking at your code, it looks like there's a path through identificationTool where cancelExecute could be called before queryDeferred has been set.
Have you got the rest of the code available?
function toolOnClick(map,qryUE,queryTaskUE) { // IDENTIFICATION $('#identify').click(function () { if(activeTool == "identify_spot") { $('#identify_spot').removeClass("active_tool"); activeTool = ""; identificationTool(false,qryUE,queryTaskUE); } else { if(activeTool != "") { $('#'+activeTool).removeClass("active_tool"); } $('#identify_spot').addClass("active_tool"); console.log("Identification: start"); identificationTool(true,qryUE,queryTaskUE); activeTool = "identify_spot"; } }); }
function identificationTool(action,qryUE,queryTaskUE) { console.log("identification? "+action) if(action == true ) { dojo.connect(map, "onClick", function(evt) { console.log("Identification: click"); qryUE.geometry = evt.mapPoint; //queryDeferred = queryTaskUE.execute(qryUE); var def = queryTaskUE.execute(qryUE, function (featureSet) { alert("FeatureSet returned"); showResults(featureSet); }, function (err) { alert("Error...but can't get at the message/reason that you expected to see, unfortunately.\n\ndef.canceled (sic) = " + def.canceled); } ); def.cancel("A reason for cancelling can be provided here",/*not strict*/false); //queryDeferred.then(showResults,showError); }); } }
/************************************************************************************ * �? COMPLETER */ function identificationTool(action,qryUE,queryTaskUE) { var def; console.log("identification? "+action) if(action == true ) { dojo.connect(map, "onClick", function(evt) { console.log("Identification: click"); qryUE.geometry = evt.mapPoint; //queryDeferred = queryTaskUE.execute(qryUE); def = queryTaskUE.execute(qryUE, function (featureSet) { alert("FeatureSet returned"); showResults(featureSet); }, function (err) { alert("Error...but can't get at the message/reason that you expected to see, unfortunately.\n\ndef.canceled (sic) = " + def.canceled); } ); }); } else { // HERE WE STOP THE EXECUTION //cancelExecute(); // this is not working because "def" is undefined even if I created it at the begining of the function. //def.cancel("A reason for cancelling can be provided here",/*not strict*/false); } } /************************************************************************************ * �? COMPLETER */ function cancelExecute() { console.log("identification queryDeferred("+queryDeferred+") to stop = "+queryDeferred.fired) if(queryDeferred && (queryDeferred.fired > 0)) { console.log("Identification: stop"); queryDeferred.cancel(); } }
May I suggest that you wrap most (nearly all!) of the identify logic into a widget/module and let it handle executing/cancelling tasks, then all your UI component needs to do is delegate to the widget/module - it will be easier to maintain state within a sensible scope with this approach.
Yes I understand, but what I want to do is when I click on my identification button, the identification process is starting. But when the user click on the identification button again (to deactivate it --> when the var action == false ) I want the queryTask to be stopped/cancelled. That is where the dojo.deferrer.cancel is suppose to be working.
But it's not.
And I can't figure out how to make it stop. In other module (like navigation toolbar) there is a method that stop the action (navToolbar.deactivate()) and this is exactly what I want to do, but with the queryTask./************************************************************************************ * �? COMPLETER */ function identificationTool(action,qryUE,queryTaskUE) { var def; console.log("identification? "+action) if(action == true ) { dojo.connect(map, "onClick", function(evt) { console.log("Identification: click"); qryUE.geometry = evt.mapPoint; //queryDeferred = queryTaskUE.execute(qryUE); def = queryTaskUE.execute(qryUE, function (featureSet) { alert("FeatureSet returned"); showResults(featureSet); }, function (err) { alert("Error...but can't get at the message/reason that you expected to see, unfortunately.\n\ndef.canceled (sic) = " + def.canceled); } ); }); } else { // HERE WE STOP THE EXECUTION //cancelExecute(); // this is not working because "def" is undefined even if I created it at the begining of the function. //def.cancel("A reason for cancelling can be provided here",/*not strict*/false); } } /************************************************************************************ * �? COMPLETER */ function cancelExecute() { console.log("identification queryDeferred("+queryDeferred+") to stop = "+queryDeferred.fired) if(queryDeferred && (queryDeferred.fired > 0)) { console.log("Identification: stop"); queryDeferred.cancel(); } }
Of course def will be undefined as the function is effectively reentrant, the previous invocation (and any variables in it) is out of scope.
You invoke the function on the first click, on the second click you invoke the function again, completely separately ('fresh' if you like) from the first invocation, this is expected.
Make sense?
function toolOnClick(map,qryUE,queryTaskUE) { // IDENTIFICATION $('#identify').click(function () { var dojoClick=dojo.connect(map, "onClick", function(evt) { identificationTool(qryUE,queryTaskUE,dojoClick,evt); }); }); } /************************************************************************************ * �? COMPLETER */ function identificationTool(qryUE,queryTaskUE,dojoClick,evt) { console.log("Identification: start"); qryUE.geometry = evt.mapPoint; def = queryTaskUE.execute(qryUE, function (featureSet) { showResults(featureSet); }, function (err) { alert("Error...but can't get at the message/reason that you expected to see, unfortunately.\n\ndef.canceled (sic) = " + def.canceled); } ); console.log("Identification: stop"); // kill the handler dojo.disconnect(dojoClick); }