3 Replies Latest reply on Nov 19, 2011 11:52 PM by ArrowUpRight

    getDefinitionByName question

    ArrowUpRight Level 2

      Greetings to everyone!

       

      construction

       

           var ClassRef:Class = getDefinitionByName("spark.components.Button") as Class;

       

      works, but can i create an instance of my custom mxml component somehow like

       

           var ClassRef:Class = getDefinitionByName("ui.edtrs.PhonesEditor") as Class;

       

      ?

       

      I've tried this, it does not work for me.

       

      Have a nice day!

        • 1. Re: getDefinitionByName question
          ArrowUpRight Level 2

          There is a strangiest workaround for this issue i've ever seen.

           

          The "solution" is:

           

          PhonesEditor

          var ClassRef:Class = getDefinitionByName("ui.edtrs.PhonesEditor") as Class;

           

          Yes, the "solution" is to place that class' name above that line.

           

          It's unbelievable.

           

          Please lions comment this.

           

          Cause for me it's not a resolving. I need that string to be passed as a value, so i do not know what to include above the line.

           

          Thanks in advance.

          • 2. Re: getDefinitionByName question
            drkstr_1 Level 4

            The problem is that if there is no reference to your class in the application, it will not be linked into the SWF when compiled.

             

            You need to "bootstrap" any class references before you can reference them dynamically. I usually do this by creating a class that holds any class references I need to create dynamically.

             

            import ui.edtrs.PhonesEditor;
            
            public class MyClassRegister
            {
                 private static var PhonesEditor:PhonesEditor;
            }
            

             

            This allows the compiler to pick up any dynamic class references so they can be included in the SWF.

            • 3. Re: getDefinitionByName question
              ArrowUpRight Level 2

              I even include an .as file with a list of my classes that i want to create dynamically:

               

              PhonesEditor

              AddressEditor

              ActionEditor

               

              and so on

               

              without declaration, just a name

               

              it's strange, but it works.

               

              Thanks.