vincent
vincent

Reputation: 1323

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 );     
        document.xmlElements.item(0).xmlElements.item("docpreset").importXML(File.applicationStorageDirectory.resolvePath("temp/cstyles.xml"));
        jsxInterface.afterImport(document);

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 {
            trace("ole");
        }

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

jsxInterface.afterImport(document);

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?

Upvotes: 0

Views: 241

Answers (2)

Loic Aigon
Loic Aigon

Reputation: 506

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

jsxInterface.init( this );

then the AS API becomes available to the js code.

Loic

Upvotes: 0

Sunil D.
Sunil D.

Reputation: 18193

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  ^^
}

[Edit]

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
    }
    else
    {
        trace("fail: i don't know what this is: ", asInterface.test);
    }

Upvotes: 1

Related Questions