Finding which layer fired an update-start event

3295
5
Jump to solution
01-13-2015 10:13 AM
james-rae
New Contributor III

When handling an update-start event from a layer, is there a way to derive which layer threw the event?  The event seems to have no args.

0 Kudos
1 Solution

Accepted Solutions
RyanClancy
Occasional Contributor

Typically your event handler function is specific to your layer so it should be self evident; in other words if you have specific functions for layer1 then you put those in the layer1 event handler, and functions for layer2 go in the layer2 event handler. But if you want some kind of visual cue you can either put some console.log statements into your event handler functions like this:

layer1.on("update-start", function(){
     console.log("layer1 update-start");
});
layer2.on("update-start", function(){
     console.log("layer2 update-start");
});

...or you can examine the evt object to get the layer id, name, url, etc.

layer1.on("update-start", function(evt){
     console.log(evt.target.id);
     console.log(evt.target.name);
     console.log(evt.target.url);
});

View solution in original post

5 Replies
RyanClancy
Occasional Contributor

Typically your event handler function is specific to your layer so it should be self evident; in other words if you have specific functions for layer1 then you put those in the layer1 event handler, and functions for layer2 go in the layer2 event handler. But if you want some kind of visual cue you can either put some console.log statements into your event handler functions like this:

layer1.on("update-start", function(){
     console.log("layer1 update-start");
});
layer2.on("update-start", function(){
     console.log("layer2 update-start");
});

...or you can examine the evt object to get the layer id, name, url, etc.

layer1.on("update-start", function(evt){
     console.log(evt.target.id);
     console.log(evt.target.name);
     console.log(evt.target.url);
});
james-rae
New Contributor III

Thanks for the reply.   My site has dynamically added layers, so having a custom hardcoded function for each is not an option.

The evt object sounds great...except it's always undefined (or is for me, anyways).  The API documentation seems to support this.  Do you actually see values in your evt param?

I have been able to hack around the issue by having the handler be defined on the layer object itself, and can then find out who raised the event using the "this" variable.  Will do for now but pretty messy.

0 Kudos
RyanClancy
Occasional Contributor

Yes, my evt object is populated with properties. When I do

console.log(evt.target.name);

... it prints the layer name in the console (firebug). And when I do

console.dir(evt);

I can explore the entire object.

james-rae
New Contributor III

I just tried a bare-bones example and yep, it has a value.  So something in my big scary app must be breaking it, but that's not the API's fault.  Thank you for your time.

0 Kudos
RyanClancy
Occasional Contributor

Good luck!

0 Kudos