经常听到有人问子窗口操作完成之后,要刷新父窗口的数据怎么办??有人用过parentDocument; 有人使用过在子窗口中new一个父窗口的对象,然后通过这个对象访问父窗口的方法或属性; 。。。。。
第一种,也可以实现。但是第二种就没法理解了,严格来说子窗口是不能new父窗口的实例的。。。。最好的方法是用事件,今天就弄一个事件的例子吧,大牛们可以不看。新童鞋务必看看,有帮助的。。。。附件可直接下载
运行页面如下
点击新增
点击保存
代码如下: 总共3个文件,一个名为Parent的application,一个名为Child的titleWindow,还有一个是自定义的一个事件。 Parent.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"?
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?xmlns:s="library://ns.adobe.com/flex/spark"?
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
- ? ?? ???<fx:Script>
- ? ?? ?? ?? ?? ? <![CDATA[
- ? ?? ?? ?? ?? ?? ?? ?? ?import mx.collections.ArrayCollection;
- ? ?? ?? ?? ?? ?? ?? ?? ?import mx.managers.PopUpManager;
- ? ?? ?? ?? ?? ?? ?? ?? ?
- ? ?? ?? ?? ?? ?? ?? ?? ?[Bindable]
- ? ?? ?? ?? ?? ?? ?? ?? ?public var ac:ArrayCollection=new ArrayCollection([
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???{name:"周结",sex:"男",age:"23"},
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???{name:"小静",sex:"女",age:"19"},serif; font-size:12px; line-height:1.8em"> ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???{name:"小二",age:"20"},serif; font-size:12px; line-height:1.8em"> ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???{name:"大牛",age:"44"}
- ? ?? ?? ?? ?? ?? ?? ?? ?]);?
- ? ?? ?? ?? ?? ?? ?? ?? ?private var child:Child = new Child();
- ? ?? ?? ?? ?? ?? ?? ?? ?protected function button1_clickHandler(event:MouseEvent):void
- ? ?? ?? ?? ?? ?? ?? ?? ?{
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???PopUpManager.addPopUp(child,this,true);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???PopUpManager.centerPopUp(child);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???child.addEventListener(MyEvent.SAVE_OK,saveSuccess);
- ? ?? ?? ?? ?? ?? ?? ?? ?}
- ? ?? ?? ?? ?? ?? ?? ?? ?private function saveSuccess(e:MyEvent):void
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???ac.addItem(e.data);
- ? ?? ?? ?? ?? ? ]]>
- ? ?? ???</fx:Script>
-
- ? ?? ???<fx:Declarations>
- ? ?? ?? ?? ?? ? <!-- 将非可视元素(例如服务、值对象)放在此处 -->
- ? ?? ???</fx:Declarations>
- ? ?? ???
- ? ?? ???<mx:AdvancedDataGrid id="adg1" designViewDataType="flat" dataProvider="{ac}" width="455" height="240" x="307" y="79">
- ? ?? ?? ?? ?? ? <mx:columns>
- ? ?? ?? ?? ?? ?? ?? ?? ?<mx:AdvancedDataGridColumn headerText="姓名" dataField="name"/>
- ? ?? ?? ?? ?? ?? ?? ?? ?<mx:AdvancedDataGridColumn headerText="年龄" dataField="age"/>
- ? ?? ?? ?? ?? ?? ?? ?? ?<mx:AdvancedDataGridColumn headerText="性别" dataField="sex"/>
- ? ?? ?? ?? ?? ? </mx:columns>
- ? ?? ???</mx:AdvancedDataGrid>
- ? ?? ???<s:Button label="新增一条" click="button1_clickHandler(event)" x="518" y="365"/>
- </s:Application>
复制代码
Child.mxml
- <s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"?
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
- ? ?? ???<s:layout>
- ? ?? ?? ?? ?? ? <s:BasicLayout/>
- ? ?? ???</s:layout>
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???var obj:Object = new Object();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???obj.name = dname.text;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???obj.age = age.text;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???obj.sex = sex.text;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???this.dispatchEvent(new MyEvent(MyEvent.SAVE_OK,obj));
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???PopUpManager.removePopUp(this);
- ? ?? ???<s:TextInput x="135" y="45" id="dname"/>
- ? ?? ???<s:TextInput x="135" y="83" id="age"/>
- ? ?? ???<s:TextInput x="135" y="127" id="sex"/>
- ? ?? ???<s:Button x="164" y="196" label="保存" click="button1_clickHandler(event)"/>
- </s:TitleWindow>
复制代码
MyEvent.as
- package event
- {
- ? ?? ???import flash.events.Event;
- ? ?? ???public class MyEvent extends Event
- ? ?? ???{
- ? ?? ?? ?? ?? ? public static const SAVE_OK:String="saveOk";
- ? ?? ?? ?? ?? ? private var _data:Object;
- ? ?? ?? ?? ?? ? public function MyEvent(type:String,data:Object=null,bubbles:Boolean=false,cancelable:Boolean=false)
- ? ?? ?? ?? ?? ? {
- ? ?? ?? ?? ?? ?? ?? ?? ?this._data=data;
- ? ?? ?? ?? ?? ?? ?? ?? ?super(type,bubbles,cancelable);
- ? ?? ?? ?? ?? ? }
- ? ?? ?? ?? ?? ? public function get data():Object
- ? ?? ?? ?? ?? ?? ?? ?? ?return _data;
- ? ?? ?? ?? ?? ? public function set data(value:Object):void
- ? ?? ?? ?? ?? ?? ?? ?? ?_data = value;
- ? ?? ???}
- }
复制代码
转自:http://www.ityangba.com/thread-302-1-1.html
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|