5 Replies Latest reply on Aug 8, 2011 8:58 AM by Andreas Jansson

    [JS] Event handler function is shown as the result in the ESTK

    Andreas Jansson Level 2

      I have a ScriptUI tree, with an onExpand event, like this:

       

      Window1.tvwTree.onExpand  = function (item){
          $.writeln(item.key + " is now expanded.");
      }

       

      Every time the script is run, I get this in the Javascript console of the ExtendScript Toolkit:

      Execution finished. Result: onExpand()
      

       

      First question is: why?

       

      Second question: What's the best and cleanest way to take care of the onExpand event? I didn't manage to assign a function to the onExpand event, in the same way as I do for parameter-less functions.

       

      For simpler events, with no parameters, I do it like this:

      Window1.....DropDownList2.onChange = cboNewLanguage_onChange;

       

       

      ... but the onExpand event has its "item" parameter to take care of, and I don't manage to assign the function in a similar way. Please enlighten me.

       

      (Would it be possible to use a prototype approach, for events like this? Getting a common function for all onExpand events...???)

       

      Thanks,

      Andreas

        • 1. Re: [JS] Event handler function is shown as the result in the ESTK
          Andreas Jansson Level 2

          *Ping* I'm adding a note here, since I believe there is knowledge out there...

           

          I would really like to know how to avoid getting the code of the onExpand event of the treeview written out as a result in the console window.

           

          Thanks,

          Andreas

          • 2. Re: [JS] Event handler function is shown as the result in the ESTK
            John Hawkinson Level 5
            Every time the script is run, I get this in the Javascript console of the ExtendScript Toolkit: "Execution finished. Result: onExpand()"

            First question is: why?

            That's easy enough. When your script ends, the resturn value of the script, if not otherwise specified, will be the value of the last assignment.

            In short, this is unremarkable and uninteresting. You could put 0; at the end of your script, I guess.

             

            Second question: What's the best and cleanest way to take care of the onExpand event? I didn't manage to assign a function to the onExpand event, in the same way as I do for parameter-less functions.

             

            For simpler events, with no parameters, I do it like this:

            Window1.....DropDownList2.onChange = cboNewLanguage_onChange;
            

             

             

            ... but the onExpand event has its "item" parameter to take care of, and I don't manage to assign the function in a similar way. Please enlighten me.

             

            What's the question here? You could certainly have defined a named function instead of an anonymous one. That is, these two bits of code are functionaly equivalent:

             

            Window1.tvwTree.onExpand  = function (item){
                $.writeln(item.key + " is now expanded.");
            }

            // OR:

            function treeExpandHandler(item) {
              $.writeln(item.key + " is now expanded.");
            }
            Window1.tvwTree.onExpand = treeExpandHandler;
            • 3. Re: [JS] Event handler function is shown as the result in the ESTK
              Marc Autret Level 4

              Hi Andreas,

               

              First question: what is supposed to be item.key? Have you extended the ListItem properties?

               

              The way you assign the onExpand function seems correct to me. The following code should work:

               

              var myExpandHandler = function(/*ListItem*/ item)
              {
                   // this => TreeView
                   // item => expanded ListItem
                   alert( item.text + ' is now expanded.' );
              };
              myTree.onExpand = myExpandHandler;
              

               

              Alternately, you can create a generic handler through the TreeView prototype:

               

              TreeView.prototype.onExpand = function(/*ListItem*/ item)
              {
                   // this => TreeView
                   // item => expanded ListItem
                   alert( item.text + ' is now expanded.' );
              };
              

               

              Works for me in CS4 & CS5+.

               

              Note: the onExpand function has no event listener counterpart, so you cannot use the form:  <widget>.addEventListener('expand', myEventListener).

               

              I don't know why your script causes the ESTK console to display "Result: onExpand()". Probably your code contains the corresponding $.writeln somewhere, doesn't it? Also, take into account that if your UI code uses sth like myListItem.expanded = true; then the onExpand handler is called before the win.show(). This may explain unexpected feedback in the console (?)

               

              @+

              Marc

              • 4. Re: [JS] Event handler function is shown as the result in the ESTK
                Marc Autret Level 4

                John Hawkinson wrote:

                 

                Every time the script is run, I get this in the Javascript console of the ExtendScript Toolkit: "Execution finished. Result: onExpand()"

                First question is: why?

                That's easy enough. When your script ends, the resturn value of the script, if not otherwise specified, will be the value of the last assignment.

                In short, this is unremarkable and uninteresting. You could put 0 at the end of your script, I guess.

                 

                 

                True!

                 

                I didn't assume that:

                Window1.tvwTree.onExpand  = function (item){
                    $.writeln(item.key + " is now expanded.");
                }

                was the actual last line of the script.

                 

                @+

                Marc

                • 5. Re: [JS] Event handler function is shown as the result in the ESTK
                  Andreas Jansson Level 2

                  The main problem was that assigning the expand handler function also returned the whole body of the expand handler function as output from the script. Since I use the console window a lot, it's very annoying to get a large number of lines of code, filling the console window, on every run.

                  I simplified the code and the question, but the reason of the question was that I wanted a console windows that didn't get cluttered on every run. In other words, this was important to me.

                   

                  And yes... It turned out to be the last line of code executed. I had been assigning this event after my main function call had created a window and returned...

                  I moved the code and used the "variable technique" demonstrated by Marc. The prototype variant also worked, I really appreciate the examples.

                   

                  Putting a zero after the function assignment would also have worked, but doing it on the right place, in the right way there is no need for dummy zeros either :-)

                   

                  (And yes, Marc: I had extended the "item" object with a "key" property.)

                   

                  Thanks,

                  Andreas