Replace Existing Event Handler

Feb 28, 2014 at 8:22 PM
I can use the connect() function to add a Javascript event handler to an event but is there a way to remove an existing handler that's defined in my C# code so that my new Javascript handler replaces the existing C# handler?

Neil

(I'm using V8)
Coordinator
Mar 2, 2014 at 11:10 AM
Hi Neil,

ClearScript does not provide this capability. Handlers for a given event generally aren't aware of each other, and the only way to do what you want in .NET is via reflection. With ClearScript you can of course allow the script to use reflection, or provide an alternate event connection API that does what you want behind the scenes.

However, some events are naturally "single-cast". That is, the semantics of the event are such that only one handler should ever be connected. If you're developing the event source, consider if that applies to the event in question. If it does, you can use custom event accessors to provide the required behavior in an elegant manner. This article demonstrates the technique.

Thanks for your question, and good luck!