Search code examples
google-closure-library

closure library generated browser events do not have target


I am trying to use closure library to build a small app. I am trying to use event delegation with following code:

var dom_ = goog.dom.$('targetelementid');
goog.events.listen( dom_, goog.events.EventType.CLICK, function( e ) {
  console.log( e );
}, false);

The problem is that when the event is dispatched and handler function is executed the generated object (goog.events.BrowserEvent ) has the target and currentTarget properties set to null. I don't understand why is that.

If I use the regular listener adding (with addEventListener ) the event passed to the handler function has the target set correctly.

Any notes on how to use the event delegation pattern in closure library code when there seem to be no target specified in the event object?


Solution

  • This is not really the case, the event have target and everything else needed at the moment when it is available in the handler, however after all registered handlers in goog.events are exhausted the event object is disposed (i.e. all its properties are deleted/null-ed) and because the modern web browser console implementations show 'live' view of the objects, the object seem to be empty.

    Hint: use 'debugger;' notion in the place where you have dounts to stop JavaScript execution and open the stack in the debugger.