14 Replies Latest reply on Jan 14, 2013 7:32 AM by Harbs.

    JSFL callbacks

    Rahul Kamra Level 1

      Hi ,

       

      I am using Adobe Extension Builder Plugin for Eclipse to build the extensions for Flash. I also want to use JSFL file in the extension. I included the .jsfl file in the Bundle editor.

       

      I can call the method of JSFL using CSInterface.instance.evalScript("hello"); and able to get the response of the method. Now i want to call a method form JSFL back to actionscript  which i was not able to do. i tried lots of ways including HostObject API but was not be able to call AS from JSFL .

       

      Any Ideas?

       

      Rahul.

        • 1. Re: JSFL callbacks
          Harbs. Adobe Community Professional & MVP

          Here's the basics:

           

          jsfl:

          function registerExtension(extension){

               kExtensionForCallback = extension;

          }

           

          function doSomethingInvolvingExtension(){

          // do stuff

          if(kExtensionForCallback){

               kExtensionForCallback.doSomeActionScriptStuff()

          } else {

               throw "woops! no extension registered!"

          }

          }

           

          ActionScript:

          CSInterface.instance.evalScript("registerExtension", this);

          CSInterface.instance.evalScript("doSomethingInvolvingExtension");

          • 2. Re: JSFL callbacks
            Harbs. Adobe Community Professional & MVP

            Sorry. This is not going to work using evalScript().

             

            It will work using the HostObject method:

            http://cookbooks.adobe.com/post_Communicating_between_JavaScript_and_the_Creative-17383.ht ml

            • 3. Re: JSFL callbacks
              Rahul Kamra Level 1

              I tried using this also. I got this error

               

              Error: EvalError: function eval must be called directly, and not by way of a function of another name

                        at TestFlash/appComplete()[D:\Work\Workspace\Extensions\TestFlash\src\TestFlash.mxml:32]

                        at TestFlash/___TestFlash_CSExtension1_applicationComplete()[D:\Work\Workspace\Extensions\Te stFlash\src\TestFlash.mxml:2]

                        at flash.events::EventDispatcher/dispatchEventFunction()

                        at flash.events::EventDispatcher/dispatchEvent()

                        at mx.core::UIComponent/dispatchEvent()[E:\dev\trunk\frameworks\projects\framework\src\mx\co re\UIComponent.as:13128]

                        at mx.managers::SystemManager/preloader_preloaderDoneHandler()[E:\dev\trunk\frameworks\proje cts\framework\src\mx\managers\SystemManager.as:2682]

                        at flash.events::EventDispatcher/dispatchEventFunction()

                        at flash.events::EventDispatcher/dispatchEvent()

                        at mx.preloaders::Preloader/displayClassCompleteHandler()[E:\dev\trunk\frameworks\projects\f ramework\src\mx\preloaders\Preloader.as:582]

                        at flash.events::EventDispatcher/dispatchEventFunction()

                        at flash.events::EventDispatcher/dispatchEvent()

                        at mx.preloaders::SparkDownloadProgressBar/initCompleteHandler()[E:\dev\hero_private\framewo rks\projects\framework\src\mx\preloaders\SparkDownloadProgressBar.as:1087]

                        at flash.events::EventDispatcher/dispatchEventFunction()

                        at flash.events::EventDispatcher/dispatchEvent()

                        at mx.preloaders::Preloader/dispatchAppEndEvent()[E:\dev\trunk\frameworks\projects\framework \src\mx\preloaders\Preloader.as:380]

                        at mx.preloaders::Preloader/appCreationCompleteHandler()[E:\dev\trunk\frameworks\projects\fr amework\src\mx\preloaders\Preloader.as:590]

                        at flash.events::EventDispatcher/dispatchEventFunction()

                        at flash.events::EventDispatcher/dispatchEvent()

                        at mx.core::UIComponent/dispatchEvent()[E:\dev\trunk\frameworks\projects\framework\src\mx\co re\UIComponent.as:13128]

                        at mx.core::UIComponent/set initialized()[E:\dev\trunk\frameworks\projects\framework\src\mx\core\UIComponent.as:1818]

                        at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\trunk\frameworks\projects\frame work\src\mx\managers\LayoutManager.as:842]

                        at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\trunk\frameworks\projec ts\framework\src\mx\managers\LayoutManager.as:1180]

              • 4. Re: JSFL callbacks
                Harbs. Adobe Community Professional & MVP

                I'm not sure I'm folowing you. What did you try?

                • 5. Re: JSFL callbacks
                  Rahul Kamra Level 1

                  <?xml version="1.0" encoding="utf-8"?>

                  <csxs:CSExtension xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:csxs="com.adobe.csxs.core.*"

                                                            applicationComplete="appComplete()">

                            <fx:Script>

                                      <![CDATA[

                   

                   

                                                [ Embed (source= "test.jsx" , mimeType= "application/octet-stream" )]

                                                private static var myScriptClass:Class;

                   

                                                [Bindable]

                                                private var hostName:String = HostObject.mainExtension;

                   

                                                public function appComplete():void{

                   

                                                          var  jsxInterface:HostObject = HostObject.getRoot(HostObject.extensions[0]);

                                                          jsxInterface.eval(new myScriptClass().toString());

                   

                                                }

                   

                                      ]]>

                            </fx:Script>

                   

                   

                   

                  </csxs:CSExtension>

                   

                  and my .jsx file looks like

                   

                  function myJSXFunction(a, b) {

                      return a + b;

                  }


                   

                   

                  It thorw error at  jsxInterface.eval(new myScriptClass().toString()); line .

                  • 6. Re: JSFL callbacks
                    Rahul Kamra Level 1

                    Am i missing somthing ?

                    • 7. Re: JSFL callbacks
                      Harbs. Adobe Community Professional & MVP

                      Probably not.

                       

                      Flash CS5 did not support HostObject.

                       

                      I thought CS6 did, but maybe I was wrong.

                       

                      Try jsxInterface.eval("alert('hello')").

                       

                      Does that pop up an alert or give you an error?

                       

                      Harbs

                      • 8. Re: JSFL callbacks
                        Rahul Kamra Level 1

                        Same Error as before,

                         

                        Error: EvalError: function eval must be called directly, and not by way of a function of another name

                         

                        But actually jsxInterface.alert("hello"); worked . Still unsuccessfull with the callbacks

                        • 9. Re: JSFL callbacks
                          Harbs. Adobe Community Professional & MVP

                          Ha!

                           

                          Okay. So it does support HostObject, but Flash does not support eval()...

                           

                          Okay. Here's how to get it to work:

                           

                          1) First use evalScript() to create your functions.

                          function init(){

                               gMyScriptInterface = function(){};

                               gMyScriptInterface.registerExtension = function(extension){

                                    gMyScriptInterface.extension = extension;

                               }

                          }

                          2) jsxInterface.gMyScriptInterface.registerExtension(this);

                          • 10. Re: JSFL callbacks
                            Rahul Kamra Level 1

                            This is what i did.

                             

                             

                            JSFL File

                             

                            function hello(){

                                 alert('hello');

                            }

                             

                             

                            Try 1:


                            [ Embed (source= "test.jsfl" , mimeType= "application/octet-stream" )]

                                                          private static var myScriptClass:Class;

                             

                             

                            I embed the .jsfl file in the MXML and then i can use CSInterface.instance.evalScript(new myScriptClass().toString())

                            Error: EvalScript: Failed to evaluate script for engine <com.example.TestFlash.extension1_Engine_Id>

                             

                            Try 2:

                             

                            I included the JSFL in the package without embeding . Using this i am able to call  CSInterface.instance.evalScript("hello");

                            • 11. Re: JSFL callbacks
                              Harbs. Adobe Community Professional & MVP

                              Use your method in try 2, but add a function to register your extension.

                               

                              DO NOT embed or use

                              CSInterface.instance.evalScript(new myScriptClass().toString())

                               

                              Call your register function directly using

                              var  jsxInterface:HostObject = HostObject.getRoot(HostObject.extensions[0]);

                              jsxInterface.gMyScriptInterface.registerExtension(this);

                               

                              You should then be able to address your extension using

                              gMyScriptInterface.extension.doSomething() fromt your jsfl script.

                              • 12. Re: JSFL callbacks
                                Rahul Kamra Level 1

                                I entore post got deleted  , let me write it again

                                • 13. Re: JSFL callbacks
                                  Rahul Kamra Level 1

                                  This is what i did.

                                   

                                   

                                  JSFL File

                                   

                                  function hello(){

                                       alert('hello');

                                  }

                                   

                                   

                                  Try 1:

                                   

                                   

                                  [ Embed (source= "test.jsfl" , mimeType= "application/octet-stream" )]

                                                                private static var myScriptClass:Class;

                                   

                                   

                                  I embed the .jsfl file in the MXML and then i can use CSInterface.instance.evalScript(new myScriptClass().toString())

                                  Error: EvalScript: Failed to evaluate script for engine <com.example.TestFlash.extension1_Engine_Id>

                                   

                                  Try 2:

                                   

                                  I included the JSFL in the package without embeding . Using this i am able to call  CSInterface.instance.evalScript("init");

                                  So i tried to changed my JSFL code and tried to pass the param

                                   

                                   

                                  var x;

                                  function init(param){

                                            x = paraml

                                  }

                                   

                                   

                                  and modified the AS code to

                                   

                                   

                                  public function appComplete():void{

                                            CSInterface.instance.evalScript("init",this);

                                  }

                                   

                                   

                                  Error: Invalid Input Parameters

                                   

                                   

                                   

                                   

                                  Try 3:

                                   

                                   

                                  Now i used a mixed way ( as mentoined by you ) keeping the same JSFL but changing the AS code

                                   

                                   

                                  AS Code :

                                   

                                   

                                  var esi:HostObject = HostObject.getRoot( HostObject.mainExtension);

                                  esi.init(this);

                                   

                                   

                                  Error : FlashPro encountered a fatal exception , FlashPro will now terminate.(I have no idea why my 2 lines of extension made the FlashPro crash).

                                   

                                   

                                  Try4:(I dont know why i dont give up)

                                   

                                   

                                  JSFL Code:

                                   

                                   

                                  var x;

                                  function init(param){

                                            x = param;

                                  }

                                   

                                   

                                   

                                   

                                  function test(){

                                            x.callB();

                                  }

                                   

                                   

                                  AS :

                                   

                                   

                                            public function appComplete():void{

                                                                          ExternalInterface.addCallback("callB",callB);

                                                                          var esi:HostObject = HostObject.getRoot( HostObject.mainExtension);

                                                                          esi.__set("x",this);

                                                                          CSInterface.instance.evalScript("test");

                                            }

                                   

                                            public function callB():void{

                                                                          trace("test");

                                            }

                                   

                                   

                                  Error: Invalid ActionScript wrapper object , Line (CSInterface.instance.evalScript("test");)

                                   

                                   

                                  However in this version i was able to set the value of x in JSFL but still was not able          get a callback

                                  • 14. Re: JSFL callbacks
                                    Harbs. Adobe Community Professional & MVP

                                    Try 3 was very close.

                                     

                                    This:

                                    var esi:HostObject = HostObject.getRoot( HostObject.mainExtension);

                                     

                                    should be:

                                     

                                    var esi:HostObject = HostObject.getRoot(HostObject.extensions[0]);

                                     

                                    But, when I try this, Flash crashes. I'm not sure what's going on.

                                     

                                    I don't really have time to spend on this now...