0

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?

Lain
  • 2,166
  • 4
  • 23
  • 47
vincent
  • 1,243
  • 4
  • 15
  • 29
  • inside afterImport() do you have access to test() function ? Meaning can you call function afterImport(document) { asInterface.test();} ? – Adrian Pirvulescu Jun 15 '12 at 11:18
  • Yes, the test function gets executed correctly, its when the test function is done that it jumps back to the jsxInterface.afterImport(document); function, and thats where i get the error – vincent Jun 15 '12 at 12:27

2 Answers2

1

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);
    }
Sunil D.
  • 17,983
  • 6
  • 53
  • 65
0

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

Loic Aigon
  • 506
  • 3
  • 1