Flex: Accessing functions / components accross mxml pages - apache-flex

For simplicity lets say I have two flex mxml pages.
form.mxml
button.mxml
If the form.mxml page had the following code, it should work fine:
<custom:SelectView dSource="{_thedata}" id="form" visible="false">
</custom:SelectView>
<mx:LinkButton label="Show" id="lbShow" click="form.visible=true;>
<mx:LinkButton label="Show" id="lbHide" click="form.visible=false;>
But if the code was like:
form.mxml
<custom:SelectView dSource="{_thedata}" id="form" visible="false">
</custom:SelectView>
button.mxml
<mx:LinkButton label="Show" id="lbShow" click="form.visible=true;>
<mx:LinkButton label="Show" id="lbHide" click="form.visible=false;>
how can I make a call from button.mxml to change form.mxml
---- a bit more details ---
My page actually looks like this: where query:AdvancedSearchFields is basically including a flex form into the page, and I want it to show/hide the custom view below after the search is complete.
<query:AdvancedSearchFields searchType="projects" searchCategory="advanced" visible="true" id="AdvancedSearch" />
<custom:SelectView dSource="{_searchResults}" id="sv" visible="false">

You could write a custom method that handles the button click events and raises a custom event. Then in form.mxml you can handle that event.
Splitting it up like this is a bit cleaner, as it makes the button.mxml file work on its own. Having Button.mxml have a direct reference to your form causes a tight-coupling between the two, and generally you should avoid tight-coupling.
EDIT: I just had another thought that also avoids tight-coupling and is a bit simpler:
form.mxml
<custom:SelectView dSource="{_thedata}" id="form" visible="{buttons.showForm}">
</custom:SelectView>
<!-- include your buttons.mxml component using an ID of "buttons" -->
buttons.mxml
<mx:Script>
<![CDATA[
[Bindable] public var showForm:Boolean = true;
]]>
</mx:Script>
<mx:LinkButton label="Show" id="lbShow" click="this.showForm=true;">
<mx:LinkButton label="Hide" id="lbHide" click="this.showForm=false;">
This essentially emulates using a custom event by using variable binding. Any time the showForm variable in buttons changes the visible property of the SelectView will be updated via the bindings. This is lighter-weight than creating a custom event (though I think custom events are a bit better of a design for it).

Your button.mxml class must have a reference to the instance of the 'form' class which will be affected. Then it can operate on it directly:
Button.mxml:
<mx:Script>
<![CDATA[
[Bindable] public var myForm:MyFormClass;
]]>
</mx:Script>
<mx:LinkButton label="Show" id="lbShow" click="myForm.form.visible=true;">
<mx:LinkButton label="Show" id="lbHide" click="myForm.form.visible=false;">
Generally, the most logical place to set this variable is in the parent of your Button class.

If you need to deal with this problem more often, I'd suggest using an MVC framework like PureMVC. It's set up so that you have a Mediator object that listens for events from MXML components, then sends a notification which can be picked up by any other mediator. Then that mediator can manipulate its own visual component based on the notification and its associated data.
In the context of what you're doing (the simple version), you're okay with the basic solution. But once you're dealing with four or five or more components with lots of logic, you will not be happy at all.

Related

'setProgress not a function' error while setting progress value of a progress bar

I want to set value of a progress bar in an accordian but I am encountering 'setProgress is not a function' error. Any idea what's wrong with following code.
Observation:
If I move the progress bar out of the Accordian then the error goes away and the progress bar appears fine.
I want to set the progress bar eventually to {repMonitor.currentItem.threatLevel} but for now I am just testing with hypothetical threat value i.e 60
<mx:Accordion id="monAccordian" includeIn="Monitoring" x="10" y="10" width="554" height="242" change="monAccordianChange()" >
<mx:Repeater id="repMonitor" dataProvider="{monitoringArray}">
<mx:Canvas width="100%" height="100%" label="{repMonitor.currentItem.firstName+' '+ repMonitor.currentItem.lastName}" >
<mx:Image x="10" y="10" source="{repMonitor.currentItem.imageName}" width="175" height="118"/>
<s:Label x="200" y="14" text="Threat Level:"/>
<mx:ProgressBar x="200" y="30" mode="manual" label="" id="bar" width="200" creationComplete="bar.setProgress(60,100);" />
</mx:Canvas>
</mx:Repeater>
</mx:Accordion>
This stems from the fact that your ProgressBar is in a repeater. You cannot reference the repeated items by id because you would have a variable number of ProgressBars with id "bar".
There are also special considerations when using event listeners inside of Repeater objects:
Event handlers in Repeater components
When a Repeater component is busy
repeating, each repeated object that
it creates can bind at that moment to
the Repeater component's currentItem
property, which is changing as the
Repeater component repeats. You cannot
give each instance its own event
handler by writing something like
click="doSomething({r.currentItem})"
because binding expressions are not
allowed in event handlers, and all
instances of the repeated component
must share the same event handler.
Repeated components and repeated
Repeater components have a
getRepeaterItem() method that returns
the item in the dataProvider property
that was used to produce the object.
When the Repeater component finishes
repeating, you can use the
getRepeaterItem() method to determine
what the event handler should do based
on the currentItem property. To do so,
you pass the
event.currentTarget.getRepeaterItem()
method to the event handler. The
getRepeaterItem() method takes an
optional index that specifies which
Repeater components you want when
nested Repeater components are
present; the 0 index is the outermost
Repeater component. If you do not
specify the index argument, the
innermost Repeater component is
implied.
You can read more about this in the Repeater docs.

Flex binding not working

I'am having problems with, binding variables being updated. I've made custom TitleWindow component which have text-input and check-box control. When the check-box changes its value, the XML in parent application should also, but it doesn't nor does the
warning: unable to bind to property
Here is the code:
<fx:Declarations>
<fx:XMLList id="nastavitve">
<nastavitve>
<zacetek omogocen="{p_zacet.selected}">
<slika>{slika_i.text}</slika>
<opis>{opis_i.text}</opis>
</zacetek>
<konec omogocen="{p_konc.selected}">
<tockovanje>{tock.selected}</tockovanje>
<kljuc>{kljuc.text}</kljuc>
<besedilo>{besedilo.text}</besedilo>
</konec>
</nastavitve>
</fx:XMLList>
</fx:Declarations>
<fx:Binding source="nastavitve" destination="parentApplication.XML_KODA.nastavitve" />
Main application:
<fx:Declarations>
<fx:XML id="XML_KODA" format="e4x" >
<shramba>
<nastavitve/>
<elementi/>
</shramba>
</fx:XML>
</fx:Declarations>
Are you using the [Bindable] property in the parent control.
Usually flex compiler will give 'Unable to bind to property' warning when the [Bindable] tag is not there.
If you are not using Bindable tag the binding may not happen correctly. We can't predict the binding process if that tag is missing. sometimes it will get bound, sometimes not.
so first check whether the bindable tag is there or not

Recreating/resetting views with Flex ViewStack

I'm writing an Adobe AIR application using a ViewStack for the different application states. Is there a way to make sure that each view component is created/destroyed each time it is shown/hidden?
For instance, if I have a TextInput in a view, I want it to reset to its initial state each time I change to that view, rather than having previously entered text. Or, if I have a Timer, I want it to be destroyed when I leave the view so that it doesn't keep running when I'm in an unrelated part of the application. I know that I can manually initialize/destroy everything on the show() and hide() events, but is there an easier way?
AFAIK there is no built-in way to do this, so you'll have to do it manually by handling the show and hide events as you mention.
ViewStack does however have two methods "saveState" and "loadState" which could perhaps help you out with this. The history manager uses these methods to enable back/forward navigation. The docs don't seem to have any examples though.
ViewStacks can be the work of the devil when it comes to creation/deletion policies and managing state. We had all sorts of problems when we developed fiat ecoDrive and by about 3/4 of the way though we we're all very anti ViewStacks for the management of view state within our application.
However... a good bet would be to first set the creationPolicy to ContainerCreationPolicy.NONE. That way it's in your control as to when to create any of the panels in your ViewStack. Then i would think you would need to have some sort of logic so that as the ViewStack changes a panel it deletes or resets the one you were on.
Another viable alternative would be to use view states instead. Have a base state which acts as the main container and then a simple state for each of your sections. That way when you switch to a new state, the old state gets removed in reverse order to the way it was created. You do have to be disciplined with states though as they can end up getting really complex and messy when they start becoming nested. If you keep it simple it may work as you require.
In MXML 2009, you can use itemDestructionPolicy="auto" to destroy a component after use it. If you use this property in the first view component with two states (init, logged), you can destroy and reinitialize all child view components. Example :
<s:states>
<s:State name="init" />
<s:State name="logged" />
</s:states>
<s:SkinnableContainer id="skincon" width="100%" height="100%" backgroundAlpha="0"
backgroundColor="#FFFFFF">
<s:VGroup id="MainContainer" width="100%" height="100%" paddingTop="0"
paddingLeft="20" paddingRight="20" gap="0">
<views:_HeaderView id="header" />
<mx:ViewStack id="viewStack" width="100%" height="100%">
<s:NavigatorContent includeIn="init" itemDestructionPolicy="auto">
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
</s:layout>
<views:LoginView title="Login" id="loginView" />
</s:NavigatorContent>
<s:NavigatorContent includeIn="logged" itemDestructionPolicy="auto">
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="top" />
</s:layout>
<views:_CentralView id="userView" />
</s:NavigatorContent>
</mx:ViewStack>
<views:_FooterView id="footer" />
</s:VGroup>
</s:SkinnableContainer>
Both answers are correct -- there doesn't seem to be any built-in way to do it. I solved the problem by creating a "wrapper" component for my view component. It creates a new view component each time the view is shown. This isn't ideal, but fits my requirements nicely, and required few changes to my application structure.
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" show="init()" hide="cleanup()">
<mx:Script>
<![CDATA[
private var myComponent:MyComponent;
private function init():void
{
myComponent = new MyComponent();
componentContainer.addChild(myComponent);
}
private function cleanup():void
{
componentContainer.removeAllChildren();
}
]]>
</mx:Script>
<mx:Canvas width="100%" height="100%" id="componentContainer" />
</mx:Canvas>
Build your "views" as separate Modules, and then use the ViewStack to switch between them. You could then write a function to destroy the unused module(s) (check each module against the selectedChild property) when the ViewStack's "change" event is fired.
2ยข
I am using different states for my different views. On each state change i add and remove components.
This causes the add and remove events of UIComponent fire which allows me to initialize and cleanup my components each time they are added.
This is the idea...
<mx:states>
<mx:State name="state1">
<mx:AddChild>
<mx:SomeUIComponent id="myComp" add="myComp.initialize()" remove="myComp.cleanup()"/>
</mx:AddChild>
</mx:State>
</mx:states>

Flex Help: Repeaters not repeating in an Accordian control

I am having a issue with databinding child repeaters inside an accordion control, hopefully you can help...
I have an accordion in a ViewStack (of which,that ViewStack is also in another top-level ViewStack). I have a repeater in each child of the accordion control. The component looks like such:
<mx:Box
xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="init()"
>
<mx:ViewStack&gt
...
<mx:Accordion creationComplete="accordianInit()">
<mx:Box label="Groups" width="100%">
<mx:Repeater id="rpGroups" width="100%">
<mx:CheckBox id="chkGroups"
label="{rpGroups.currentItem.name}" />
</mx:Repeater>
</mx:Box>
<mx:Box label="Contacts">
<mx:Repeater id="rpContacts">
<mx:CheckBox id="chkContacts"
label=quot;{rpContacts.currentItem.full_name}" />
</mx:Repeater>
</mx:Box>
</mx:Accordion>
...
</mx:ViewStack&gt
<mx:Box>
The problem is that if I bind the 2 repeaters in the init function, then both repeaters don't show any data. If I bind the repeaters in the accordianInit function, then only the first repeater (rpGroups) gets databound...
Where should I be data binding the repeaters so that both repeaters repeat properly?
Hopefully this makes sense, if not I can elaborate more, any help is appreciated.
Bind the dataProvider of the repeater to the source in MXML itself:
<mx:Repeater dataProvider="{the_data}" ... />
The reason you're seeing the behavior you are is because the Accordion (and ViewStack) does not create all of it's children right away. It only creates the child that is visible (so, the first Box, and the first ViewStack child initially).
Because of this behavior, when you try to assign data to the repeaters in the first init() event handler the repeaters have no instantiated container to repeat children into. When you assign data to the repeaters in accordionInit() then only the first Box has been created, which is why only the first repeater works.
If you don't want to bind to the data via the dataProvider attribute of the Repeater tag (as I've shown above), then you can use a change handler on the Accordion to set the repeater data as the user changes panes (because as the user clicks into the panes, they get created by the Flex framework).
All of this comes about from the creationPolicy property: http://livedocs.adobe.com/flex/3/html/layoutperformance_05.html

Dynamic RadioButtons

Our resident Flex expert is out for the day and I thought this would be a good chance to test this site out. I have a dropdown with a dataProvider that is working fine:
<ep:ComboBox id="dead_reason" selectedValue="{_data.dead_reason}"
dataProvider="{_data.staticData.dead_reason}"
labelField="#label" width="300"/>
The ComboBox is custom but I'm not sure if that matters for the question. I need to change the combo box to radios (all in one group) but maintain the dynamic options. In other words, what is the best way to generate dynamic RadioButtons?
Try using an <mx:Repeater> Something like:
<mx:Repeater dataProvider="{_data.staticData.dead_reason}">
<mx:RadioButton groupName="reasons" ...>
</mx:Repeater>
<mx:RadioButtonGroup id="RDO_Group"/>
<mx:Repeater id="myRepeater" dataProvider="{_data.staticData.dead_reason}">
<mx:RadioButton id="rdo" label="{myRepeater.currentItem}" value="{myRepeater.currentItem}" groupName="RDO_Group"/>
</mx:Repeater>
is the best way.

Resources