tags:

views:

1991

answers:

3

Hello everyone.I've been searching this on the net but i haven't find an answer. I have array data from title window which i need to pass directly to main window after closing the title window.

Heres my code on main window

    private function showWindow():void 
    {
    var dataCntnrsForTxt: Array = new Array;
    var ttlWindow:addQuest=addQuest(PopUpManager.createPopUp(this, addQuest, true));

    pointer.x=btnaddQuestion.x;
    pointer.y=btnaddQuestion.y;                
    pointer=btnaddQuestion.localToGlobal(pointer);
    ttlWindow.x=pointer.x-500;
    ttlWindow.y=pointer.y;


    ttlWindow.dataCntnrsForTxt= dataCntnrsForTxt;                      

    var i: int;
    for (i = 0; i < dataCntnrsForTxt.length; i++)
{
  var lblshow: Label = new Label;
      lblcntnrs.addChild(lblshow);
      lblshow.text = dataCntnrsForTxt[i];    
    }


   }

i want to display automatically the result to main window after closing the title window

heres the code on title window

  [Bindable]
  public var dataCntnrsForTxt: Array = new Array;

  private function trythis():void
  {
  var i:int;
  for (i = 0; i < contnrs.numChildren; i++)
   {
    dataCntnrsForTxt.push(TextInput(contnrs2.getChildAt(i)).text);         
   }
  PopUpManager.removePopUp(this);
  }

I am a newbie on flex programming. Thank you in advance for your help

A: 
var dataCntnrsForTxt: Array = new Array;

This is a local variable, it's scope is limited to the showWindow() function. Once, you are out of it, you are not supposed to play with it. Or, else you could return this from the function (instead of void use Array).

[Bindable]
public var dataCntnrsForTxt: Array = new Array;

This is better. You can now, access this from anywhere. Note, it is always better to use an ArrayCollection if you are going to bind it. Here's Flex 3.2 documentation:

When defining a data binding expression that uses an array as the source of a data binding expression, the array should be of type ArrayCollection because the ArrayCollection class dispatches an event when the array or the array elements change to trigger data binding. For example, a call to ArrayCollection.addItem(), ArrayCollection.addItemAt(), ArrayCollection.removeItem(), and ArrayCollection.removeItemAt() all trigger data binding.

Can you try with an ArrayCollection?

dirkgently
+3  A: 

I recommend dispatching an event when the title window is closed. The event can also store the values to be returned in an attribute. Then, handle that event in your main app.

David Hanak
+2  A: 

Events are a common approach as another answer says and good because they decouple your title window from the main window. They are a good habit to get into in Flex and worth the effort, so that's probably the best answer.

Making your array a public bindable variable on the main window and filling it from your title window is a second way, but I don't like this approach because the two components know too much about each other and you are relying on data binding which is likely to do more than you actually need (for instance if you bind a control in your main window to the same array it will be busy updating in the background while your title window is showing. That means you have a fiddly "Cancel" to deal with if you need one).

Just to give you a third approach, you can pass a function to your title window which gets called when the window closes. e.g. in your Title window you have the declaration...

public var onClose:Function;

write yourself a handler on the title window, bound to the close event which calls the external function and passes the array...

private function doClose():void
{
    if (onClose != null) onClose(myArray);
}

The caller then has to have a function that can be called...

private function handleTitleWindowClose(myArray:Array):void
{
    // do something in here
}

and you pass that to the TitleWindow class before you show it...

ttlWindow.onClose = handleTitleWindowClose;

This is probably not as good as an event, but better than a shared magic variable. It is not a clear API because the arguments to onClose are not documented anywhere and the compiler won't pick up an error in the argument list, but it is useful pattern to know.

HTH

Simon
Thank You. It works fine now...
Jejad