Assigning events to existing CLR objects

Nov 25, 2011 at 4:38 PM

Hi guys,

I'm rather new to Jint, but it seems like a really great product! I'm wondering how I can assign events to an exposed CLR object through Jint? An example would be a WPF Button control in my interface, where I want to assign a function to the Click event using JavaScript code executed by Jint. I have tried quite a few variations, but nothing seems to work. What is the syntax for hooking up an event and handling it with JacvaScript code?

Kind regards,

Kasper

Developer
Nov 25, 2011 at 7:13 PM

Hi,

yes it's possible to subscribe to the events,

var obj = new MyProject.SomethingWithEvent();
obj.add_MyEvent(function(){
    // handle your event here
});

Nov 25, 2011 at 9:04 PM
Edited Nov 25, 2011 at 9:12 PM

Hi cin,

Thank you so much for your fast response. Your suggestion is working as expected. However, I would like to know if this is the "official" way of doing it or if there are other ways, perhaps one more like the .NET way of assigning an event, with the += operator? Just curious here :)

Also, is it possible to remove an event again, once assigned? I tried with remove_EventName, and even with a non-anonymous function, but it seems that the event handler is kept.

Developer
Nov 26, 2011 at 6:58 AM

add_Event and remove_Event expect you to pass a delegate, Jint implicitly  creates an anonymous .net wrapper for the js function each time a delegate is required. since delegates are not the same you can't remove the event handler.

Nov 26, 2011 at 9:47 AM

Okay, but is it possible to supply a real delegate instead, so that I'm able to remove it again? Or is that not possible with Jint?

Developer
Nov 26, 2011 at 8:07 PM

Yes, you can use a real delegate, but I haven't tested this case...

Aug 16, 2012 at 4:48 AM
cin wrote:

Hi,

yes it's possible to subscribe to the events,

 

var obj = new MyProject.SomethingWithEvent();
obj.add_MyEvent(function(){
    // handle your event here
});

 

Sorry about starting up an old thread, however I'm also new to jint and was wondering if this is still the correct way to do events. I believe I've done everything correctly, however it appears that add_<event_name> is not defined. Just to be clear, if I have an event, for example, named "OnChat" in a variable "Client" would this be correct?

Client.add_OnChat(function() {
    // Handle OnChat event
});

 

Once again, sorry for starting up an old thread. Thank you for your time!