2 Replies Latest reply on Jun 6, 2011 3:40 AM by pauland

    Can't access object using "id" or "name" if created with actionscript

    DanStarlight Level 1

      How can you register an instance of an object with actionscript so that it's id or name value is accessible?

       

      I included a simple example where a Button is created using mxml and in the same way it is created using actionscript.  The actionscript object is inaccessible using it's "id" and "name" property.

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
                     creationComplete="application1_creationCompleteHandler(event)">
          <fx:Script>
              <![CDATA[
                  import mx.events.FlexEvent;

       

                  protected function application1_creationCompleteHandler(event:FlexEvent):void
                  {
                      import spark.components.Button;
                     
                      var asBtn:Button = new Button();
                      asBtn.label = "actionscript";
                      asBtn.x = 200;
                      asBtn.id = "asButton";
                      asBtn.name = "asButtonName";
                      addElement(asBtn);
                     
                      trace("mxmlButton="+this["mxmlButton"].label); // returns: mxml  label
                      //trace("mxmlButton="+this["asButton"].label); // returns runtime error: ReferenceError: Error #1069: Property asButton not found on TestId and there is no default value.
                      //trace("mxmlButton="+this["asButtonName"].label); // returns runtime error: ReferenceError: Error #1069: Property asButtonName not found on TestId and there is no default value.
                     
                  }

       

              ]]>
          </fx:Script>
          <s:Button
              id="mxmlButton"
              label="mxml label"
              alpha="0.8"/>
      </s:Application>

        • 1. Re: Can't access object using "id" or "name" if created with actionscript
          blazejewicz Level 4

          Hello,

           

          #1

          "id" is property that is translated into "name" of display object - but only if created in mxml - then it is converted into name in generated actionscript and your component will have "name" property that equals that "id" at runtime I think.

          So if you set "id" via mxml - then it is translated (in the background by compiler) and you could access it directly via dot-notation in actionscript

          #2

          But if you create component (UIComponent instance) at runtime with code - then "id" property is not directly translated into "name" property id DisplayObject - as there is no generated actionscript by compiler. So to avoid that do not use "id" when using actionscript - or re-assign id to name.

          #3

          Also do not expect in #2 scenario that your "name" property will be available in dot-like notation (via brackets) in scope of given component - this could not necessairly be correct as in #1 scenario. Instead query via name given container (this could be very different control in mx/sparks), see below:

          protected function addButtonHandler(event:MouseEvent):void

          {

               var btn:Button = new Button();

               btn.label = "Button";

               btn.id = "myButton"+getTimer();

               btn.name = btn.id;

               btn.addEventListener(MouseEvent.CLICK, buttonClickedHandler);

               panel.addElement(btn);

          };

          protected function buttonClickedHandler(event:MouseEvent):void

          {

               var btn:Button = event.target as Button;

               var instance:Button = panel.contentGroup.getChildByName(btn.id) as Button;

               trace(btn === instance);

          }

          (=== should yield true If I'm not wrong with this and "name" equals "id")

           

          regards,

          Peter

          • 2. Re: Can't access object using "id" or "name" if created with actionscript
            pauland Level 4

            Hi Dan,

             

            It is a very rare occurrence when I miss not being able to access an object (object property, really) using the ["name"] notation for objects created using actionscript.

             

            In MXML the compiler is conveniently adding an attribute to the class with the same name as the id, so you can conveniently refer to it using the [] notation. While we explicitly specify an application container to use, the MXML compiler creates a custom container which is a derivative of the base container and to that it adds properties for the children declared in MXML. I guess it also effectively calls "addElement" for us when  the container is being constructed.

             

            Your example assumes that using "addElement" to add the button to the application container is the same as declaring a variable (ie property ). It isn't, so there's no point in looking for an property of the name "as3Button" using the [] notation, because it doesn't exist. The container is managing a collection of children in it's display list and that's not the same as being accessible as properties of the container.

             

            Generally speaking, accessing properties using the ["name"] syntax isn't necessary.

             

            Paul

             

            [edit: you may wonder why "addElement" doesn't conveniently also add the "id" attribute to be an property of the container class. Unfortunately, it can't because the container class would need to be dynamic and it's not. A further complication would be that adding properties at runtime would invite naming clashes at runtime with associated mayhem. MXML can do this because the compiler generates the class and can trap name duplication at compile time.

             

            Great question, BTW.

             

            -last edit changed my "attributes" to be "properties" in line with Adobe's terminology]