PDA

View Full Version : Initializing containers...how to?


kahuja
04-10-2008, 03:44 PM
I am stuck with a very peculiar problem or so I think. My steps:

1. Login to the application and view a canvas which has some control, the data is being fed from the backend. The logic is placed in "creationCompete" event.

2. Now user logs out and we move out of the view (we are using <mx:ViewStack/>)

3. User logs in again and now i want to run the same logic as it was run earlier on the "creationComplete".

Problem: As the container has already been created, the event is not fired and the code does not execute.

What we cannot do: We can not fire custom events because the views are dynamic and firing a common event would mean that all the children would dispatch the same at the same time. This is against the deferred construct of the viewstack.

Help?

sleekdigital
04-10-2008, 04:01 PM
If I understand your question ... Could you use the view stack's change event instead of ( or possibly in addition to) the inner containers creationComplete? And in the change handler make your logic conditional based on "selectedChild" property.

kahuja
04-10-2008, 04:18 PM
If i read correctly, I would register an event on change on a view stack and then fire en event that contains the selectedChild. All the children, while listen to the property, will only act upon if it is for them?

If the answer is yes, how do I propagate the event all the nested children from there on?

sleekdigital
04-10-2008, 04:41 PM
Doesn't sound like we are on the same page, perhaps you could provide sample code that illustrates your problem?

kahuja
04-10-2008, 04:59 PM
Code for my view that holds the Viewstack:



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:view="com.sapient.resultspace.view.*" creationComplete="init();" activate="">

<mx:Script>
<![CDATA[
import mx.core.UIComponent;
import com.adobe.cairngorm.control.CairngormEvent;
import com.adobe.cairngorm.control.CairngormEventDispatch er;
import mx.controls.Alert;
import mx.events.IndexChangedEvent;


private function next():void
{
if(vs.selectedIndex == 3)
{
vs.selectedIndex = 0;
}
else
{
vs.selectedIndex = vs.selectedIndex + 1;
}
}

private function viewChanged(event:IndexChangedEvent):void
{
var e:CairngormEvent = new CairngormEvent("Navigate");
var comp:UIComponent = vs.getChildAt(event.newIndex) as UIComponent;
e.data = comp.id;

Alert.show(e.data); // Vill happen only after one round trip
e.dispatch();
}
]]>
</mx:Script>

<mx:VBox>
<mx:Button label="Move next" click="next();"/>
</mx:VBox>


<mx:ViewStack id="vs" selectedIndex="0" width="100%" height="100%" change="viewChanged(event);">
<view:MyCanvas id="c1" label1="Stack 1" />
<view:MyCanvas id="c2" label1="Stack 2" />
<view:MyCanvas id="c3" label1="Stack 3" />
<view:MyCanvas id="c4" label1="Stack 4" />
</mx:ViewStack>
</mx:Application>

-->


Code for my component


<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import com.adobe.cairngorm.control.CairngormEvent;
import com.adobe.cairngorm.control.CairngormEventDispatch er;

private function init():void
{
CairngormEventDispatcher.getInstance().addEventLis tener("Navigate", handle);
}

private function handle(event:CairngormEvent):void
{
if(event.data == this.id)
{
Alert.show("View " + this.id + " will handle");
}
}
]]>
</mx:Script>

</mx:Canvas>

sleekdigital
04-10-2008, 05:22 PM
Ok, so can't you create a public method in your component and call it in the component init method and also call it from your viewChanged handler in the view?

kahuja
04-10-2008, 05:26 PM
1. Good question.... seems like i am overwhelmed with Events

2. On thinking, to call a method, i would have to write strong-typed references in the main app and that can lead to tight-coupling (this was an example, but in my case the children would be of different types).

3. What were you suggesting - you mentioned about not being on the same page

sleekdigital
04-10-2008, 05:30 PM
2. On thinking, to call a method, i would have to write strong-typed references in the main app and that can lead to tight-coupling (this was an example, but in my case the children would be of different types).

Can you make all view types that can be used in this situation implement an interface that includes this method? To me that would loosen up the coupleing to a satisfactory level :)

But I would think your main app is already coupled to these since they are referenced by the viewstack...

<view:MyCanvas id="c1" label1="Stack 1" />

sleekdigital
04-10-2008, 05:34 PM
3. What were you suggesting - you mentioned about not being on the same page

I guess we were on the same page for the most part, but some of waht you said in post #3 made me unsure.

kahuja
04-10-2008, 05:36 PM
I knew "interfaces" would be next - somehow trying to follow one approach, but i agree makes sense to use interfaces here.

About coupling, because i have <mx:MyCanvas/> but that is bare minimum you need, how can you un-couple that? I heard that there is some reflection support not sure if it is possible and needed.

Thanks for the help.

kahuja
04-10-2008, 05:38 PM
Would you also be able to help me with another thread i started: http://www.actionscript.org/forums/showthread.php3?t=166456