Same event handler for multiple maps

788
2
02-18-2014 08:04 PM
JosephSaltenberger
New Contributor III
Is there a way to identify which map instance fired the event handler in the following example? I could set up separate event handlers, but in this case, I will have many maps, and would prefer to keep the handler to one function. Thanks

var mapExtentChange = map.on("extent-change", changeHandler);
var mapExtentChange = map2.on("extent-change", changeHandler);

function changeHandler(evt){
   //if map1 fired then...
   map1.extent = evt.extent;
   //else
   map2.extent = evt.extent;
}
0 Kudos
2 Replies
RobertoPepato
Occasional Contributor II
Is there a way to identify which map instance fired the event handler in the following example? I could set up separate event handlers, but in this case, I will have many maps, and would prefer to keep the handler to one function. Thanks

var mapExtentChange = map.on("extent-change", changeHandler);
var mapExtentChange = map2.on("extent-change", changeHandler);

function changeHandler(evt){
   //if map1 fired then...
   map1.extent = evt.extent;
   //else
   map2.extent = evt.extent;
}


You can inspect the evt.target.container.id property to discover who raised the event.
0 Kudos
JosephSaltenberger
New Contributor III
Thanks, that works. I ended up doing this though:

var mapExtentChange = map.on("extent-change", function(evt){
    changeHandler(evt, "map")
});

var mapExtentChange = map2.on("extent-change", function(evt){
    changeHandler(evt, "map2")
});


function changeHandler(evt, mapX){
    //do stuff here with mapX.....
}
0 Kudos