12 Replies Latest reply on Sep 21, 2015 8:43 PM by CarlosCanto

    Multi Level Object

    aiScripting Level 1

      Ok, so this is probably really easy and I am just missing something....

      but since y'all are so helpful on here I'll just ask!

       

      I am trying to do something like this:

      function getOptions(){
          var  b = {};
          b.Tfinsih.type = topBO.selection.text;
          b.Tfinish.pocket = tPocket.text;
          b.Bfinish.type = bottomBO.selection.text;
          b.Bfinish.pocket = bPocket.text;
          b.Lfinish.type = leftBO.selection.text;
          b.Lfinish.pocket = lPocket.text;
          b.Rfinish.type = rightBO.selection.text;
          b.Rfinish.pocket = rPocket.text;
          
          return b;
      };
      

       

      Thanks

      -Boyd

        • 1. Re: Multi Level Object
          CarlosCanto Adobe Community Professional & MVP

          can you elaborate?

          • 2. Re: Multi Level Object
            Silly-V Adobe Community Professional

            Yea, what exactly seems to be the issue?

            • 3. Re: Multi Level Object
              aiScripting Level 1

              Ok guys! sorry I should have made this a little more clear.

               

              function getOptions(){  
                  var  b = {};  
                  b.Tfinsih = true; // This works
                  b.Tfinsih.type = topBO.selection.text;  // but as soon as I try to add another dimension to the object like this it doesn't work
                  b.Tfinish.pocket = tPocket.text;  
                    
                  return b;  
              }; 
              

               

              So I want to be able to set a value (a bool in this case) to the first level of the object the "Tfinish" but then I want to have several other properties to the "Tfinish" like "type" and "pocket" etc that will all have values too.

              Its probably something simple but for some reason I can't think of the right way to do this. I guess maybe I am trying to add an object to an object? ... idk anyway hope this helps you understand what I am trying to do here.

              -Boyd

              • 4. Re: Multi Level Object
                Silly-V Adobe Community Professional
                1. b.Tfinsih = true; // This works 
                2.     b.Tfinsih.type

                 

                Looks like you are assigning a property to a boolean value, which does not, by definition have properties.

                You'd have to do something like this:

                b.Tfinish = {finished: true};

                b.Tfinish.type = ...

                • 5. Re: Multi Level Object
                  aiScripting Level 1

                  Hmmm....

                  but when I try to assigns something to b.Tfinish.type nothing happens. if I change it to not have the second '.' then it works like I would expect because there is only one level after the "b" my object.

                  I just know I have seen code like this:

                   

                  sw = doc.spots.add();  
                  sw.colorType = ColorModel.SPOT;  
                  sw.name = n;  
                  sw.color.cyan = c;  
                  sw.color.magenta = m;  
                  sw.color.yellow = y;  
                  sw.color.black = k; 
                  

                  Where "sw" is the object and there are properties like "colorType" etc. but like the color has multiple properties so you have "sw.color.cyan" etc. I want to be able to do something like this.

                  I am basically trying to gather as much information into this object to pass to another function. And seeing this code I know there are ways to do something like I am thinking. But I am just not sure where I am going wrong.

                  Thanks!

                  -Boyd

                  • 6. Re: Multi Level Object
                    Silly-V Adobe Community Professional

                    Still unclear why you are having difficulties.. is not your goal similar to the below?

                     

                        function MyObject(customAttribute){
                            var b = {};
                            b.nameProp = {firstName: "Hello"};
                            b.nameProp.lastName = "World";
                            b.nameProp.customAttribute = customAttribute;
                            return b;
                        };
                    
                    
                        var myObj_a = MyObject();
                        alert(myObj_a.nameProp.firstName + " " + myObj_a.nameProp.lastName); // Hello World
                        alert(myObj_a.nameProp.customAttribute); // undefined
                       
                        var myObj_b = MyObject("Cupcakes");
                        alert(myObj_b.nameProp.firstName + " " + myObj_b.nameProp.lastName); // Hello World
                        alert(myObj_b.nameProp.customAttribute); // Cupcakes
                    
                    • 7. Re: Multi Level Object
                      CarlosCanto Adobe Community Professional & MVP

                      Would this work for you?

                       

                      b.Tfinsih.value = true;

                      • 8. Re: Multi Level Object
                        aiScripting Level 1

                        So Yeah when I tried to assign something to "b.Tfinish.value" it wouldn't do it.... (as in nothing happend, it didn't throw an error or crash the code. Just nothing after that in the function executed).

                        now I'm getting more confused

                        • 9. Re: Multi Level Object
                          aiScripting Level 1

                          No thats not exactly what I am after.

                          I want something more like:

                              function MyObject(customAttribute){  
                                  var b = {};  
                                  b.nameProp = true;  
                                  b.nameProp.lastName = "World";  
                                  b.nameProp.customAttribute = customAttribute;  
                                  return b;  
                              };  
                          
                          
                              var myObj_b = MyObject("Cupcakes");  
                              if(myObj_b.nameProp){
                              alert(myObj_b.nameProp.lastName); // Hello World  
                              alert(myObj_b.nameProp.customAttribute); // Cupcakes  
                              };
                          

                          But that just comes back undefined

                          • 10. Re: Multi Level Object
                            Silly-V Adobe Community Professional

                            Okay, to re-iterate, you can't set properties to boolean "true".  true doesn't hold any properties.

                            It has to be like this:

                            b.nameProp = {};

                             

                            now, nameProp is an object, and you can put properties into it to your content.

                            If you want to use the if statement, it will still work without nameProp being the boolean "true".

                            Just by virtue of existing, nameProp will execute your if statement, and if it does not exist as a property of b (b.namePrep), it will not execute your if statement. This should be the answer you are looking for, right?

                            • 11. Re: Multi Level Object
                              aiScripting Level 1

                              Maybe what I think I want to do isn't exactly possible...

                              But I was hoping to have like

                              b.nameProp1

                              and

                              b.nameProp2

                              etc. etc.

                              with each one of those having there own properties

                              like

                              So this Kinda works:

                                  function MyObject(){  
                                      var b = {};  
                                      b.hi = "hi"
                                      b.nameProp1 = {};  
                                      b.nameProp1.lastName = "World1";  
                                      b.nameProp1.firstName = "Hello1";
                                      b.nameProp2 = {}; 
                                      b.nameProp2.lastName = "World2";
                                      b.nameProp2.firstName = "Hello2";
                                      return b;  
                                  };  
                              
                              
                                  var myObj_b = MyObject();  
                                  if(myObj_b.nameProp1){
                                  alert(myObj_b.nameProp1.firstName + " " + myObj_b.nameProp1.lastName + " " + myObj_b.hi); // Hello1 World1 hi
                                  };
                              
                              
                                  if(myObj_b.nameProp2){
                                  alert(myObj_b.nameProp2.firstName + " " + myObj_b.nameProp2.lastName); // Hello2 World2   
                                 
                                  };
                              

                              I guess I was mostly missing that I need to create an object for each second level like: "b.nameProp1 = {};" .....


                              I am trying to gather info about all 4 sids of a banner thats why the "b" .... so I have a UI I am working on that will have options for top, bottom, left, right. Like a drop down list with some options, and a edit text box for a pocket dimension, etc etc. For each side.

                              So I was hoping to gather all that info and put it into one object that I can send to other functions. So I could access any property of any side of the banner.

                              • 12. Re: Multi Level Object
                                CarlosCanto Adobe Community Professional & MVP

                                sorry I wasn't in front of my computer earlier, here's what I suggested, pretty much what SillyV said and what I think you know by now,

                                 

                                here's how it works using your first sample

                                 

                                function getOptions(){   
                                    var  b = {};   
                                    b.Tfinish = {};
                                    b.Tfinish.value = true; // This works 
                                    b.Tfinish.type = 'type';
                                    b.Tfinish.pocket = 'pocket text';
                                       
                                    return b;   
                                }; 
                                
                                
                                var opts = getOptions ();
                                
                                
                                alert(opts.Tfinish.type);
                                

                                 

                                 

                                ...and here's using your second sample

                                 

                                function MyObject(customAttribute) {
                                    var b = {};
                                    b.nameProp = {};
                                    b.nameProp.value = true;
                                    b.nameProp.lastName = "World";
                                    b.nameProp.customAttribute = customAttribute;
                                    return b;
                                };
                                
                                
                                
                                
                                var myObj_b = MyObject("Cupcakes");
                                if (myObj_b.nameProp) {
                                    alert(myObj_b.nameProp.lastName); // Hello World    
                                    alert(myObj_b.nameProp.customAttribute); // Cupcakes    
                                };