JavaScript events

Nov 16, 2013 at 7:04 AM
Dear James,

is it possible to allow the JavaScript code to bind event handlers?

If so, what CLR-Code would be neccessary to allow the following JS code to work?
myobject.myevent = function() { alert("Event has been fired."); }
        public override void Initialize()
            this.AsDynamic.myevent = ...

        public override void SomeOtherFunction()
I tried several approaches using AsDynamic, Properties and Acessors, but my best result was that the program crashed - without any kind of error message ;-)

Thanks again!
Nov 16, 2013 at 6:07 PM

Sorry, events are not bound. I never had time to put this in just yet. The best workaround I can offer is to create methods such as "addEventListener()/removeEventListener()" on your object, and call those from the script environment just like they do in client browsers for hooking up events.
Nov 16, 2013 at 6:09 PM
Another work around: Just create a set-only property that accepts a function. When you set the public property, it sets a private event property.
Jan 3, 2014 at 6:19 AM
Hello James,

from my perspective, this doesn't just look like a workaround, more a solution! Thanks.

Short question:

This code works flawlessly, the V8Function stored in the InternalHandle onClick is being executed:
Because I'd like to fill the "this" variable within the JS function with a sensible value, I'd like to handover the current object (derived from V8ManagedObject). The following throws a System.AccessViolation-Exception within V8.Net.dll.
                    onClick.Call(this, InternalHandle.Empty);
Am I using the function in an unintended way or may this be a bug?

I am using the current V8.NET Release v1.4.0.1 from the download page. Please tell me, if you'd like to have any more information!

Jan 3, 2014 at 4:41 PM
Edited Jan 3, 2014 at 4:43 PM
It may be because you are passing empty as a parameter value; just leave it out. That said, I should support that as one would expect, so it may be a bug. If it works without empty, feel free to make a bug/issue entry.
Jan 30, 2014 at 10:13 PM
Hello James,

this code works fine, where returnValue is of type Handle:
returnValue = possibleFunction.AsInternalHandle.Call(_this.AsInternalHandle, new InternalHandle[] { });
One question though: While return values of the JavaScript function are returned into the managed side (as returnValue), if a JavaScript error happens, returnValue is empty.

Is there any way to retrieve the error text of JavaScript errors happened?

Thank you!

All the best
Jan 31, 2014 at 4:24 AM
Edited Jan 31, 2014 at 4:25 AM
If an exception is caught in script, the handle will be a string, which is the error message; It shouldn't be empty.
Jan 31, 2014 at 4:30 AM
Edited Jan 31, 2014 at 4:32 AM
Oh, you are calling a function - sorry, I didn't wrap that. Put it in as a feature request, and I'll try to get it in asap. For now, call using "Execute()" instead, which traps the error.
Jan 31, 2014 at 7:02 AM
Okay, thanks a lot James!
Jan 31, 2014 at 3:10 PM
No problem, any time! ;)