Re: Can I create my own User events?
- From: Serge Baltic <nsr@xxxxxxxxxxx>
- Date: Thu, 02 Jun 2005 10:43:38 -0700
Hello. I know how to setup events for actions based on html elements, but is it possible to create my own? For instance, if I execute a function, then I can write a line of code to execute my own specialized event. I would also like to pass it parameters.
Unfortunately, on both JScript and JScript.NET it's not possible to define own events. however, you may override an event derived from a base class.
However, if you do not need to interop with other languages, you may simply emulate the events infrastructure. Events are no more than a syntax sugar, you know.
For example, you want to define an event called "Happens". Add those things:
• Some storage for the event handlers. These would be delegates, either of ..NET or ActiveX style, depending on the situation. This could be a list, an array, or a hash map.
• A function for subscribing to event, the standard name would be add_Happens(handler). Add the handler to the list.
• A function for unsubscribing from the event (optional), the standard name would be remove_Happens(handler). Remove the handler from the list. Note that .NET compares the handles semantically, not by instances.
• A function to fire the event. For example, OnHappens.
How when someone wants to subscribe for your events, he should call the add_Happens(MyHandler), where MyHandler is a memeber function with the arguments appropriate.
When the event source wants to fire the event, it calls the OnHappens with some arguments to be casted along with the event. The OnHappens function enumerates the handlers list and invokes each one with the appropriate set of arguments.
L> An "event" is something that happens, like a mouse click. L> It's not something that you can execute.
It's a wrong understanding of the events nature. One who handles the mouse click executes the event (fires) so that the event handlers could sink it. You may as well call onclick() on the HTML element (or click() ? don't remember). Also, the event may fire as a result of an async operation completion, and so on. It's just an abstract conscept, like function, property, member variable, public/protected members, and so on, with its own use cases.
(H) Serge
.
- Prev by Date: Re: How to hide script code from user?
- Next by Date: Re: Sending an SMTP Email
- Previous by thread: Re: Can I create my own User events?
- Next by thread: Re: Getting mouse position within div during click event?
- Index(es):
Relevant Pages
|