2 Replies Latest reply on Jun 23, 2011 12:29 AM by ShpigelMania

    override public set data

    ShpigelMania Level 1

      Hi,

       

      I've been programming in Flex for a while, and so has a colleague of mine. Today we found ourselves arguing over a rather basic issue. One of us (Mr. Yellow) wrote something like this:

       

      ---------

       

      <s:ItemRenderer>

      <fx:Script>

      <![CDATA[

      override public function set data(value:Object):void
      {
      super.data = value;
      // look here
      a_label.visible = value && value.showImage;
      }
      <s:Label id="a_label" text="Now you see me now you don't"/>
      </s:ItemRenderer>
      ----------
      The other (Mr. Blue) said it was risky setting the visible property on 'a_label' for fear it might be null. Mr. Yellow insists that 'set data' will only be called by an IItemRendererOwner when it's certain that it has been added to the display list and so setting a property on it is safe and cites examples in
      to support his case, but Mr. Blue insists that the usual invalidateProperties->commitProperties pattern is in order.
      Is Mr. Yellow being rash or is Mr. Blue being pedantic?
      Many Thanks,
      Mr. Green

        • 1. Re: override public set data
          UbuntuPenguin Level 4

          I usually just have bindable variables in my itemrenderers so the visual component can get the data whenever it feelsl like it

           

          [Bindable] public var grillName:String;
          [Bindable] public var cooksName:String;
          [Bindable] public var patioBackgroundColor:int;
          override public function set data( value:Object ):void
          {
             super.data = value;
             if( value != null )
             {
                 customGrill   = value as Grill;
                 grillName  = customGrill.grillName;
                 cooksName = customGrill.grillName;

                 if( cooksName != 'Ubuntu Penguin'  )
                 {
                     patioBackgroundColor= Color.RED  <--stolen from java
                 }
                 else
                 {
                     patioBackgroundColor= Color.Coolasacucumber;
                 } 
             }
             else
             {
                  ///Clear out stuff here
              }
          }

          <Label text={'The grills name is ' + grillName}/>
          ...
          <SolidFill color={patioBackgroundColor} />
          ....

           

           

          I agree with Mr. Blue that setting a property on a visual object that may not exist is kind of risky. Basically it boils down to wanting to do something that we are unsure of whether it works, or using something where we know it works.  I will say that I have never seen the null point exception thrown from an itemrenderer but I have seen them thrown from other non-recyclable components when a child subcomponent is accessed.

          • 2. Re: override public set data
            ShpigelMania Level 1

            Hi Penguin,

             

            Thanks for your reply. Mr. Yellow agrees that setting a property on a visual object that may not exist is kind of risky. He argues that in the 'set data()' (as in commitProperties()) the visual object are assured to exist. The documentation is not entirely clear on this and one is left to imply from the examples. Maybe someone from the Flex SDK team can make this clear.

             

            Mr. Green