2 Replies Latest reply on Nov 19, 2012 7:40 AM by Charles Hamlyn

    Trouble with Functions and Scope

    Charles Hamlyn

      I have a function "createBoard" declared under "compositionReady".  I can call it from anywhere in compositionReady with a simple "createBoard();"  I can even assign it on the fly to dynamic symbols with "newSymbol.bind("click",function(){createBoard();});" 

       

      However, if I set the click event to call the function from a static symbol or element on the Stage, It always gives me an "LOG: Javascript error in event handler! Event Type = element" error.  I've tried all of the following:

       

      createBoard();

      sym.getComposition().getStage().createBoard();

      sym.getComposition().getStage().getSymbolElement().createBoard();

      sym.getComposition().getSymbolElement("Stage").createBoard();

      this.createBoard();

       

      and a handful of other ones.  Most of those are from scouring the Edge Animate Preview forums.

       

      Anyone have any insight on what I'm doing wrong?

       

      Here's the compositionReady code:

      ------------------------------------------

                //Edge symbol: 'stage'

         (function(symbolName) {

               

                          Symbol.bindElementAction(compId, symbolName, "document", "compositionReady", function(sym, e) {

       

       

                                              //FUNCTIONS:

                                              //Function to create the board.

                                              function createBoard(){**SNIP**}

      ...

      ------------------------------------------

       

      and Here's the static symbol code:

      ------------------------------------------

            Symbol.bindElementAction(compId, symbolName, "${_title}", "click", function(sym, e) {

               // insert code for mouse click here

               sym.getComposition().getSymbolElement().createBoard();

              

            });

            //Edge binding end

      -----------------------------------------

        • 1. Re: Trouble with Functions and Scope
          joel_pau Level 5

          Hi,

           

          Your function is available, and only available, within compositionReady. You  have set a local function.

          If you want to use this function on an click event, you have to add a prefix: "sym."

          Therefore, you have to declare sym.createBoard() on compositionReady rather than createBoard().

          Then, using your action click, you call it using: sym.createBoard(); Your action click is within stage symbol.

          You can download an example here: http://forums.adobe.com/thread/1099475?tstart=0. See post #6, click on New file attached v4.

           

          ------------------------------------------

          //Edge symbol: 'stage'

          (function(symbolName) {

          Symbol.bindElementAction(compId, symbolName, "document", "compositionReady", function(sym, e) {

          //FUNCTIONS:

          //Function to create the board.

          sym.createBoard = function (){**SNIP**}

          ...

          //later, you call it using:

          sym.createBoard();

           

          ------------------------------------------

          and Here's the static symbol code:

          ------------------------------------------

          Symbol.bindElementAction(compId, symbolName, "${_title}", "click", function(sym, e) {

          // insert code for mouse click here

          // Here you call it using:   

          sym.createBoard();

          });

          //Edge binding end

          -----------------------------------------

           

          ==> You can also use sym.setVariable() and sym.getVariable() to set and get a global function.

          From Edge API:

          function localFunction () { console.log("myFunction called is: localFunction()"); }

          sym.setVariable("myFunction", localFunction);

           

          ==> More about global functions: see here.

          • 2. Re: Trouble with Functions and Scope
            Charles Hamlyn Level 1

            Thanks Joel.  I had tried declaring it as "sym.createBoard();" but I think I must've continued calling it with just "createBoard();" because that prevented my being able to call it even within compositionReady.

             

            I'll try your suggestion this afternoon when I return home.

             

            Incidentally, I did look into using get/setVariable and aside from not getting that to work either, I couldn't see what benefit there is.  It just seems like an extra line of code whenever I need to call a function.  Any insight on why someone would be better off using that method than simply sym?

             

            Thank you for the links.  I've reviewed both of those and these great posts:

             

            http://forums.adobe.com/message/4419690#4419690#4419690

            http://forums.adobe.com/message/4423625

            http://forums.adobe.com/message/4768297

             

            and aside from the conflicting or merely different ways of coding this, I couldn't quite put the information in the context of my own project.  I plan to learn this thoroughly and then post a lengthy tutorial with lots of visuals on what belongs to what and which channels are used to access the different pieces.  I'll post a link to it here and other places once I get it done.

             

            Thank you again for your help.