apache-flex - 弹性 : Programmatic equivalent of tag-based event handlers?

标签 apache-flex events actionscript event-handling

当我创建如下内容时:

<mx:DataGrid id"myDataGrid"
             itemEditBegin="myDataGrid_itemEditBeginHandler(event)" />

何时添加和删除“itemEditBegin”的事件监听器?这本质上与以下内容相同吗:

<mx:DataGrid id="myDataGrid"
             creationComplete="myDataGrid_creationCompleteHandler(event)" />

protected function myDataGrid_creationCompleteHandler(event:FlexEvent):void
{
    this.myDataGrid.addEventListener(DataGridEvent.ITEM_EDIT_BEGIN,
                                     this.myDataGrid_itemEditBeginHandler);
}

protected function myDataGrid_whatEventDispatcherGoesHere?Handler(event:FlexEvent):void
{
    this.myDataGrid.removeEventListener(DataGridEvent.ITEM_EDIT_BEGIN,
                                        this.myDataGrid_itemEditBeginHandler);
}

基本上,我想知道如果我想以编程方式执行此操作,应该在哪里添加“myDataGrid.addEventListener”?它应该在对象本身的creationComplete监听器函数中,还是在它所在的任何父对象的creationComplete监听器函数中?

最佳答案

如果您以编程方式添加事件监听器:

  • 对象的creationComplete处理程序是一个好地方
  • 如果您以编程方式创建 dataGrid,只需在实例化对象后随时添加即可
  • 将其放入父级的creationComplete 处理程序中会给您的代码增加不必要的复杂性,我不建议这样做。不过,它会起作用的。

在 Flex 中创建对象的良好引用:http://www.mikaflex.com/?p=270

关于apache-flex - 弹性 : Programmatic equivalent of tag-based event handlers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4959064/

相关文章:

javascript - 单例无法跨多个文件 Nodejs 工作

java - dbpedia/flash 如何?

apache-flex - 检测 YouTube 是否被公司/ISP 阻止

apache-flex - 减少条形图系列的宽度

actionscript-3 - Flash 安全权限在 Chrome 中不起作用

actionscript-3 - Flex 4 皮肤和绑定(bind)

javascript - CodeMirror OnSelect 事件不起作用

javascript - 在按下某个字符后调用函数,但前提是手前有空格

actionscript-3 - ActionScript 3 中的计时器

apache-flex - 如何识别类引用是否是接口(interface)?