从操作更新外部Flex组件

时间:2010-04-06 20:39:05

标签: flex events addeventlistener dispatchevent

我是Flex的新手,我很难理解事件。我认为事件是我想要用于我的情况。我有2个组件addUser.mxmllistUsers.mxml。我从主应用程序中的ViewStack访问这些。当我加载listUsers.mxml时,它会通过HTTPService调用显示数据网格中的当前用户列表。当我使用addUser.mxml上的表单添加用户时,我希望当我返回到该视图以显示新用户时,listUsers.mxml中的数据网格会刷新。我使用addEventListenerdispatchEvent尝试了几种不同的功能,但似乎无法使其正常工作。有人可以帮我解释这个逻辑吗?

-

评论示例代码,我已经解析了非相关内容。

adduser看起来像这样:

<mx:HTTPService id="httpService" 
        url="{'http://someurl.com'}" 
        useProxy="false" 
        method="POST"
        fault="faultHandler()"      
        result="resultHandler(event)"
         />


public function addUser():void{  
            if(validateForm()){
                params = {};
                params['action'] = 'adduser';
                params['firstName'] = firstName.text;           
                params['lastName'] = lastName.text;
                params['email'] = email.text;
                params['isActive'] = isActive.selected;

                httpService.cancel();   
                httpService.addEventListener("result", addUserResult);                      
                httpService.send(params);
            }
}

public function addUserResult(event:ResultEvent):void{
            var result:Object = event.result;

            //reset fields if add user was successful
            if(result.returnXML.action=='adduser'){

                var m:String = result.returnXML.message.toString();                                 
                    if(result.returnXML.status=='fail'){                        
                        Alert.show(m, null, Alert.OK, null, null, Application.application.IconError);
                    }
                    if(result.returnXML.status=='success'){                     
                        firstName.text = "";            
                        lastName.text = "";
                        email.text = "";
                        isActive.selected = true;

                        Alert.show(m, null, Alert.OK, null, null, Application.application.IconSuccess);
                    }                   
            }                   
}   


<mx:Button id="addButton" label="Add" click="addUser();" />

listUsers看起来像这样:

<mx:HTTPService id="httpListService" 
        url="{'http://someurl.com'}" 
        useProxy="false" 
        method="POST"
        fault="faultHandler()"      
        result="resultHandler(event)"
         />


<mx:DataGrid id="dgUsers"                         
                itemClick="dgClickEvent(event);"                          
                width="85%" 
                maxHeight="500"             
                >

                <mx:columns>
                    <mx:DataGridColumn headerText="First Name" dataField="fn" />
                    <mx:DataGridColumn headerText="Last Name" dataField="ln" />
                    <mx:DataGridColumn headerText="Email" dataField="email"  />
                    <mx:DataGridColumn headerText="Active" dataField="active" />
                </mx:columns>
            </mx:DataGrid>

2 个答案:

答案 0 :(得分:0)

我认为事件听众不一定是要走的路。您可以使用事件侦听器在检测到其他内容时执行某些操作。 ie)在ui组件上监听鼠标=检测鼠标向下,执行拖动操作。

考虑到你的例子,我认为你只需要将你的功能链接在一起。看起来你的addUser函数将用户保存到列表用户从中获取数据的相同源,因此在你的位置我会在添加用户结果的末尾调用listUsers httpService来刷新填充数据网格的数据。

httpListService.send()

我没有看到httpListService的结果处理程序,但是你在那里更新dataGrid中的数据。

祝你好运,请回复任何并发症。

答案 1 :(得分:0)

搞定了。这就是我所做的 - 基本上每当父视图堆栈将listUsers视图聚焦时,它就会发送httpListService并刷新datagrid,而不管addUser组件或任何其他组件中的任何事件(或非事件)。它增加了网络流量,但对于我的项目范围,这是可以接受的。

listUsers.mxml中的

<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">

...

public function init():void{
    //vsUsers is my view stack on the main application component
    Application.application.vsUsers.addEventListener(IndexChangedEvent.CHANGE, refreshUsersGrid);           
}

...

public function refreshUsersGrid(e:IndexChangedEvent):void{     
    //if the new viewable child is the list users view, then refresh the datagrid   
    if(Application.application.vsUsers.getChildAt(e.newIndex).name=='viewListUsers'){               
        //the sendListUsersRequest method fires the HTTPService send method and binds the results to the datagrid
        sendListUsersRequest();
    }
}