2 Replies Latest reply on Oct 4, 2006 12:02 PM by inlineblue

    Conditional Binding

    Mark Forsberg Level 1
      I would like to do conditional binding, i.e. if a value exists then bind one way, else bind the other. When I attempt to do it the Flex Engine throws many errors. Here is one of the things that I tried:

      bind="{(gCurrentUsers.selectedItem == undefined) ? {newFirstName.text.substring(0,1).toLowerCase()}{newLastName.text.toLowerCase()}@mySite.c om: {gCurrentUsers.selectedItem.NETWORK_NAME}@mySite.com}}"

      Is it in fact possible to do conditional binding?

      Mark F.
        • 1. Re: Conditional Binding
          gladders Level 1
          You can do 'conditional binding', but you need to move your logic into a function and set the value of a bindable variable. For example:


          private var foo:String

          private function bar(foobar:String):void


          <mx:Label text="{foo}" />
          <mx:Button click="bar('hello')" label="Hello" />
          <mx:Button click="bar('goodbye')" label="Goodbye" />


          • 2. Re: Conditional Binding
            inlineblue Level 1
            What you're doing should work, but you have to remember some things:

            - Only one set of burly braces is used
            - Everything inside the braces is supposed to be a valid ActionScript expression that evaluates to a single value.

            Using the ternary operator (?:) should be perfectly fine, but you have to keep it legal:

            { gCurrentUsers.selectedItem == null ?
            newFirstName.text.substring(0,1).toLowerCase() + newLastName.text.toLowerCase() + '@mySite.com' :
            gCurrentUsers.selectedItem.NETWORK_NAME + '@mySite.com' }

            Personally, I think something this complex should be done via a function:

            private function getEmail(user: Object): String
            if (user == null)
            return newFirstName.text.substring(0,1).toLowerCase() + newLastName.text.toLowerCase() + '@mySite.com';
            return user.NETWORK_NAME + '@mySite.com';

            and bind like this: