Some time we need to trigger(call) a function of different application(parent) while an event occurs in child application. Lets say, we need to select an item from available item list. To select an item, a new window is created and it lists available items. The user selects an item. Once the user selected an item, it should be reflected in parent window.
I have created a function called
changeItem() in
ParentWindow. Since the function is triggered by an event, it should have a variable of type Event.
The code of the
changeItem() is
public function changeItem(event:Event,inItem:String):void
{/* variable inItem is passed by the ChildWindow */
txtSelectedItem.text=inItem;
}(To create and call a window please see
multiple windows in AIR )
Pass the application as a parameter.When creating the instance of the
ChildWindow in the
ParentWindow, declare a variable of type
ParentWindow and initialize to "this" i.e current application. The code is given here
declare variableprivate var thisInstance:ParentWindow;calling the ChildWindow/*newChildWindow is of type ChildWindow*/newChildWindow=new ChildWindow();/* intialize the variable thisInstance in init() function if you are referring the intansce in multiple places*/thisInstance=this;
newChildWindow.parentWindowInstance=thisInstance;
newChildWindow.open();In the
ChildWindow (select item window), add a listener to the select button/component and call the function of the Parent window (
changeItem).
add event listener functioncmd.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
{/* parentWindowInstance variable is the instance of the ParentWindow and it is initialized by the ParentWindow when the instance of the ChildWindow is createdparentWindowInstance is declared in ChildWindow <Script> section */
parentWindowInstance.changeItem(e,pTxtInput.text);
}here, a function is declared in the parameter place. This declaration acts as parameter passing trick.