Flex倾听事件问题

时间:2009-08-04 23:13:56

标签: flex

我对flex非常陌生,并试图弄清楚如何监听我创建的自定义事件。让我先介绍一下我的应用程序背景。

我的目录结构如下。

     
  • 资产  
  • 部件  
  • 控制  
  • 事件  
  • 模型  
  • util的

在模型类(可绑定)中,我有一个ArrayCollection,它将通过Web服务调用加载数据,特定组件中的datagrid将绑定到该ArrayCollection并显示数据。通过按钮按下调用Web服务,该按钮启动搜索事件。

model/ApplicationModel.as

[Bindable]
public class ApplicationModel
{
    //put specific model components here
    public var data:ArrayCollection;


    //the one instance in this Singleton
    private static var _instance:ApplicationModel;

    public function ApplicationModel()
    {
        if (_instance != null) throw new Error("ApplicationModel already exists!");
    }

    public static function getInstance():ApplicationModel
    {
        if (_instance == null) _instance = new ApplicationModel();
        return _instance;
    }

}

components/SearchBox.mxml

<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
    <![CDATA[
        import com.homedepot.di.tp.SCVTools.events.*;

        private function doSearch():void
        {
            var query:String = searchTI.text;

            //only want to dispatch an event if there is something to query
            if (query) dispatchEvent(new CustomEvent(CustomEvent.UPDATE_START,query));
        }
    ]]>
</mx:Script>
    <mx:Label  text="Enter Search Term"/>
    <mx:TextInput id="searchTI" enter="doSearch()" />
    <mx:Button label="Search" click="doSearch()"/>
</mx:HBox>

[Bindable] public class ApplicationModel { //put specific model components here public var data:ArrayCollection; //the one instance in this Singleton private static var _instance:ApplicationModel; public function ApplicationModel() { if (_instance != null) throw new Error("ApplicationModel already exists!"); } public static function getInstance():ApplicationModel { if (_instance == null) _instance = new ApplicationModel(); return _instance; } }

此搜索功能正常,将返回我想要的数据。我需要知道的是,当完成此Web服务调用时,视图组件可以相应地更新视图的其他方面(根据返回的数据隐藏datagrid的列作为示例)。

我的Application.mxml文件将连接我的控制器以侦听CustomEvent。然后,控制器将委派工作以实际调用Web服务并获取数据。委托将创建一个HTTPService对象并获取数据。它还将处理HTTPService的结果。我目前正在尝试在处理HTTPService调用结果的函数中调度一个新事件。这似乎没有用,因为事件永远不会冒泡到我的视图组件。

<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ import com.homedepot.di.tp.SCVTools.events.*; private function doSearch():void { var query:String = searchTI.text; //only want to dispatch an event if there is something to query if (query) dispatchEvent(new CustomEvent(CustomEvent.UPDATE_START,query)); } ]]> </mx:Script> <mx:Label text="Enter Search Term"/> <mx:TextInput id="searchTI" enter="doSearch()" /> <mx:Button label="Search" click="doSearch()"/> </mx:HBox>

snippet of control/DataUpdateDelegate.as

我尝试在Application.mxml文件中连接此UPDATE_END事件,但这似乎也不起作用。

有没有人对我的视图如何监听未从子组件调度的事件,而是从对视图组件一无所知的ActionScript类有任何建议?

谢谢,

托马斯

1 个答案:

答案 0 :(得分:0)

当您在视图中首次设置data时,可以使用BindingUtils将特定功能绑定到ApplicationModel属性:

public function set model(applicationModelInstance:ApplicationModel):void
{
    _applcationModelInstance = applicationModelInstance;
    BindingUtils.bindSetter(dataRefreshed, _applicationModelInstance, "data");
}

protected function dataRefreshed(value:Object):void
{
    // do whatever you need to do here. 
    // value==applicationModelInstance.data
}

取消注册这样的绑定以避免内存泄漏有点棘手,但由于你的ApplicationModel是一个单身人士,所以无论如何都不需要。