Actionscript error when executing eventlistener in javascript

I have the following code using actionscript and the indesign sdk:

At the beginning of my Class

 [ Embed (source= "resources/js/eventHandlers.jsx" , mimeType= "application/octet-stream" )]
        private static var jsHandler:Class;
        var  jsxInterface:HostObject = HostObject.getRoot(HostObject.extensions[0]);

In my function:

        jsxInterface.eval( new jsHandler().toString());
        jsxInterface.init( this );     

this is the code inside eventHandlers.jsx:

var asInterface = {};
function init(wrapper) {
  asInterface = wrapper;

function afterImport(document) {
    document.addEventListener (Document.AFTER_IMPORT, asInterface.test());

and from javascript I call this:

public function test():void {

Now, the test function gets executed correctly, but after that the next thing that gets called is again this:


and then an error is thrown:

Error: ActionScript error: Error: Missing required parameter 'handler' for method 'addEventListener'.

I have no idea anymore on what I need to do. All I want is for the xml to be imported and an event dispatched when the import is complete.

I have no idea why I even have to call a javascript function, and can't use the document.AFTER_IMPORT inside a normal eventListener. Can anyone help me out on this one please?


The problem seems to be that in your afterImport() method, you are adding the event listener incorrectly.

The second parameter to the addEventListener() method should be the name of a function (technically it's a reference to a function). In your code, you have put parentheses on the end of the function name -- so instead of providing the reference to the function, the function is being executed.

When the function executes, it returns nothing, so the second parameter to addEventListener() is missing and you get the error.

Try this instead:

function afterImport(document) {
    document.addEventListener (Document.AFTER_IMPORT, asInterface.test);
    //                                           notice no "()" here  ^^


Since you are still getting the error, you might want to add some code to debug it further.

In the method above, comment out the addEventListener line, and replace with something like this:

    // for debugging
    if (asInterface.test == null)
        trace("fail: it's null");
    else if (asInterface.test is Function)
        trace("success: it's a function");
        // this is the only code path where your error will not occur
        trace("fail: i don't know what this is: ", asInterface.test);

I think you have to initiate the AS wrapper like this :

jsxInterface.init( this );

then the AS API becomes available to the js code.


Need Your Help

How to change text of a SKLabelNode?

ios sprite-kit sklabelnode

How do I update a label's text that was created in initWithSize?

Reducing Number of Params in Factory Class GetObj

c# parameters factory factory-pattern

This is a general question, which is why there is not much code below. I have looked on stackoverflow and do not see the solution (could have missed it).