Hi Gordon,

I can see the point. It is indeed confusing at times... but still, I
have built a primitive implementation and it has made my life much
easier.

Think about this simple but common use case:

I have to generate form from an XML spec. As i iterate through the
form item nodes I have to instantiate the correct controls ( may be
datechoosers, textinputs, etc ).
I would really like to keep everything in MXML ( i am trying to use as
much binding, repeaters, etc as possible to keep things concise and
easier to mantain ) but I am missing the ability to conditionally
instantiate one control or another.

The following class allows me to do conditional instantiation using
MXML, and it has saved me TONS of code. It is still work in progress
but it makes the work for now. Here's a small tada list off the top of
my head

* extend repeater or sth like that so it adds children to the parent container
* allow for runtime changing of the selectedIndex
* figure out some binding issues and creation/destruction policies

any help and ideas.. more than welcome




                public class Switch extends HBox
                {
                
                        private var _selectedIndex:int;
                        
                        public function get selectedIndex():int
                        {
                                return _selectedIndex;
                        }
                        /**
                         * if changed at runtime, for now we are doing nothing
                         * we can try sth later...
                         *
                         * @param value
                         *
                         */             
                        public function set selectedIndex( value:int ):void
                        {
                                trace( "set selectedIndex " + value );
                                _selectedIndex = value;
                        }
        
        
                        override public function 
createComponentsFromDescriptors(
recurse:Boolean = true ):void
                        {
                                
                                if ( childDescriptors.length == 0 )
                                        return; // no children specified
                                
                                // remove current children
                                
                                if ( selectedIndex != -1 && childDescriptors[ 
selectedIndex ] )
                                {
                                        
                                        trace( "createComponentFromDescriptor " 
+ selectedIndex );
                                        
                                        createComponentFromDescriptor( 
childDescriptors[ selectedIndex ]
, recurse );
                                }
                                
                                validateNow();
                                
                        }               
        
        
                }





<conditionals:Switch
        selectedIndex="{getSwitchIndexForFormItemNode(rep2.currentItem)}"
        paddingBottom="0" paddingLeft="0" paddingRight="0" paddingTop="0"
        >
        
        <ui:SuperStyleFormItem_ColorPicker data="{rep2.currentItem}"/>
        
        <ui:SuperStyleFormItem_Heading data="{rep2.currentItem}"/>
        
</conditionals:Switch>





















On 5/31/07, Gordon Smith <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
>
>
> > c'mon adobe  guys! you can do it ;)
>
> When  we implemented <mx:Repeater>, we considered whether to add conditional  
> tags like <mx:If>. We decided against it on the grounds that it would be  
> confusing about whether these tags worked at compile time or at runtime. We 
> felt  that dynamic runtime logic belongs in script, not in declarative  
> syntax.
>
> -  Gordon
>
>
>  ________________________________
 From: [email protected]  [mailto:[EMAIL PROTECTED]
On Behalf Of Aldo  Bucchi
> Sent: Thursday, May 31, 2007 5:41 PM
> To:  [email protected]
> Subject: [flexcoders] Re: Brainstorming;  Dynamic Conditionals in MXML
>
>
>
>
>
>
> ok,
>
> just realized that you can accomplish something similar by  using
> navigators... just set creationPolicy="auto" and  conditionally set the
> selectedIndex... only it will just work under some  particular
> circumstances and is quite hacky.
>
> i need to destroy  children when the condition changes, to avoid
> parameter problems... but  still. that only leaves me with binding
> issues.
>
> OTOH, the ideal way  would be to create a custom component that
> implements IRepeater so that  containers don't add it, and then play
> with  createChildrenFromDescriptors and such.
>
> i just gave the latter a try  and got it kinda working, but it would
> take toooooooooo much time to make it  work with bindings and stabilize
> it. i give up.
>
> c'mon adobe  guys!
> you can do it ;)
>
> best,
> Aldo
>
> On 5/31/07, Aldo Bucchi  <[EMAIL PROTECTED]>  wrote:
> > Hi Guys,
> >
> > Has anyone flirted with the idea of  creating MXML
> > conditional-structure components that can be used to  dynamically
> > generate interfaces??
> >
> > just to be more  graphical... something like this
> >
> > <mx:Repeater id="rep2"  dataProvider="{rep1.currentItem.elements('*')}">
> >
> >  <foo:If  condition="[EMAIL PROTECTED]'super'}">
> >
> >  <foo:SuperLabel  text="[EMAIL PROTECTED]"/>
> >
> >  <foo:Else>
> >
> > <mx:Label  text="[EMAIL PROTECTED]"/>
> >
> >  </foo:Else>
> >
> > </foo:If>
> >
> >  </mx:Repeater>
> >
> > it just makes a lot of sense...
> > any  ideas????
> >
> > best,
> > Aldo
> >
> >
> > --
> >  ::::: Aldo Bucchi :::::
> > mobile +56 9 8429 8300
> >
>
> --
> ::::: Aldo Bucchi :::::
> mobile +56 9 8429 8300
>
>
>                   



-- 
::::: Aldo Bucchi :::::
mobile +56 9 8429 8300

Reply via email to