开发者

Flex: Programmatic equivalent of tag-based event handlers?

开发者 https://www.devze.com 2023-02-09 14:29 出处:网络
When I create something like the following: <mx:DataGrid id\"myDataGrid\" itemEditBegin=\"myDataGrid_itemEditBeginHandler(event)\" />

When I create something like the following:

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

When does the event listener for "itemEditBegin" get added and removed? Is this essentially the same as:

<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_itemEdi开发者_高级运维tBeginHandler);
}

Basically, I'm wondering where I should add "myDataGrid.addEventListener" if I want to do it programmatically? Should it be in the creationComplete listener function of the object itself, or perhaps in the creationComplete listener function for whatever parent object it resides in?


If you're adding the event listener programmatically:

  • creationComplete handler of the object is a good place for it
  • If you're creating the dataGrid programmatically, just add it anytime after instantiating the object
  • Putting it the in the Parent's creationComplete handler adds unnecessary complexity to your code, and I wouldn't recommend it. However, it will work.

Good reference for object creation in Flex: http://www.mikaflex.com/?p=270

0

精彩评论

暂无评论...
验证码 换一张
取 消