1 Reply Latest reply on Oct 29, 2014 6:39 PM by vivekuma

    Recycle code and dynamic data

    QuadrantProductions Level 1

      resdesign helped me create a function earlier today which I just tidied up a bit below…. my code was sloppy.

      It got me thinking, what if you wanted to recycle the code.

       

      1) Is it logical to assume that this would make the file load time even faster.

      2) It would make the project / object / method scalable from project to project.

       

      So for example, lets say the following controls a drop menu

       

      var A=1;

      var mymenu = sym.getSymbol("themenu")

      mymenu.$("thebttn").click(function(){

        if(A==0){

        mymenu.playReverse('closeit');

        A=1;

        }else{

        mymenu.play('openit');

        A=0;

        }

      });

       

       

      What if you wanted now to say you had 2 iterations (or any other number for that matter) of the same menu object but it was going to load in unique data.  How would or even could you automate that?

      Or would you have to write out the code block twice like….

       

      var A=1;

      var mymenu = sym.getSymbol("themenu")

      mymenu.$("thebttn").click(function(){

        if(A==0){

        mymenu.playReverse('closeit');

        A=1;

        }else{

        mymenu.play('openit');

        A=0;

        }

      });

       

      //second menu

      var B=1;

      var mymenu2 = sym.getSymbol("themenu2")

      mymenu2.$("thebttn").click(function(){

        if(B==0){

        mymenu2.playReverse('closeit');

        B=1;

        }else{

        mymenu2.play('openit');

        B=0;

        }

      });

       

       

      mymenu.stop("openit");

      mymenu2.stop("openit");

       

      I've added the base file set up that I used to create this at this link for anyone to view / modify https://www.dropbox.com/sh/farjoa32vnv0fyh/AACOpnerkKNdSSH1cqW_Q3bPa?dl=0

      I'll pull them down in about a week though. just keeping things tidy.

        • 1. Re: Recycle code and dynamic data
          vivekuma Adobe Employee

          Add the below code in the stage compositionReady

          sym.btnHandler = function(menu, btn, closeMark, openMark) {
            var A = 1;
            var mymenu = sym.getSymbol(menu);
            mymenu.$(btn).click(function(){
            if(A == 0) {
            mymenu.playReverse(closeMark);
            A = 1;
            }
            else {
            mymenu.play(openMark);
            A = 0;
            }
            });
            return mymenu;
          }
          

           

          Then whereever you want to use it, use it like below: (if sym refers to stage, as we have added the above function as an attribute to stage object)

               sym.btnHandler("themenu", "thebttn", 'closeit', 'openit');

           

          hth,

          Vivekuma