7 Replies Latest reply on Feb 26, 2008 8:03 AM by tsnevillecom

    Passing mx:Label id to function

    tsnevillecom
      OK... To show my novice capabilities in Flex, I have a rather simple question... so I think?

      I have a form with the element:

      <mx:FormItem label="Time">
      <mx:Label text="{getTime(this.id)}" id="TimeStamp" width="160"/>
      </mx:FormItem>

      For some reason, "this.id" is sending the name of my mxml document instead of "TimeStamp". How would I go about dynamically passing the name of the mx:Label to the function.

      Thanks in advance.
      ~T
        • 1. Re: Passing mx:Label id to function
          Gregory Lafrance Level 6
          Not sure what you're trying to do. The label displays the time, but where should the input to the getTime() function come from?
          • 2. Re: Passing mx:Label id to function
            tsnevillecom Level 1
            The getTime function is defined in the CDATA as:

            private function getTime():String{
            var now:Date = new Date();

            if (now.month<10){
            var month:String = "0"+now.month.toString();
            }else{
            month = now.month.toString();
            }
            if (now.date<10){
            var day:String = "0"+now.date.toString();
            }else{
            day = now.date.toString();
            }
            if (now.hours<10){
            var hours:String = "0"+now.hours.toString();
            }else{
            hours = now.hours.toString();
            }
            if (now.minutes<10){
            var minutes:String = "0"+now.minutes.toString();
            }else{
            minutes= now.minutes.toString();
            }
            if (now.seconds<10){
            var seconds:String = "0"+now.seconds.toString();
            }else{
            seconds = now.seconds.toString();
            }
            var year:String = now.fullYear.toString();
            var time:String = now.toLocaleTimeString () ;
            var date:String = year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
            return date;
            }

            I just want to know how to pass the id of the Label that is calling the function dynamically without having to put each individual id within the tag. The purpose for this is to eventually set up case statements depending on id. Does that make any sense?
            • 3. Re: Passing mx:Label id to function
              jylaxx Level 1
              First : You can use Formatter class (DateFormatter) to replace the getTime code.
              Second : I don't really understand what you want to do. Why the getTime function need to acces to the Label object ?
              • 4. Re: Passing mx:Label id to function
                tsnevillecom Level 1
                OK...maybe this getDate example is not a good one. Say I had button with an id. What I want to do is pass the button id (i.e. "click=someFunction(buttonID)") to the function. If it's a label, button, or whatever... I would just like to dynamically send the id of whatever element is calling the function. Does that make sense? Like I said, I'm a novice and used to AS2. For now, I'll set a simple function to:

                public function someFunction(elementID):void{
                trace (elementID);
                }

                God, I hope this makes sense. I'm just used to Flash and I'd normally call someFunction(this._name).

                Thanks... and sorry if I'm confusing as h*ll.
                • 5. Re: Passing mx:Label id to function
                  tsnevillecom Level 1
                  Also, thanks for the DateFormatter tip. I'm reading about that now.
                  • 6. Re: Passing mx:Label id to function
                    jylaxx Level 1
                    When using events methods you use an Event object parameter. Events classes include information about the object dispatcher (see doc).
                    "click=someFunction(event)"
                    In the someFunction code you can use the event.target property which is the object Button.

                    For other functions you can use the object ID as a var instance.

                    • 7. Re: Passing mx:Label id to function
                      tsnevillecom Level 1
                      Perfect... That's exactly what I was looking for. I owe you a beer.

                      Thanks.