开发者

Adobe flex - Event metaData Tag and clone method

开发者 https://www.devze.com 2023-03-22 10:58 出处:网络
I have few questions related to events. Can anyone explain the exact need of overriding clone() while creating custom events?

I have few questions related to events.

Can anyone explain the exact need of overriding clone() while creating custom events? I read in the Flex Cookbook that we need to override clone() just in case we want to redispatch this event. Does this mean when we want event开发者_Python百科 to be bubbled up the display hierarchy, the dispatched event should be our custom cloned event and not the Event object?

Second, what is the need of the metadata tag:

[Event(name="modelEvent", type="com.abc.data.model.ModelEvent")]

public class LoginModel extends EventDispatcher {}

I understand we need to extend EventDispatcher in case we want to dispatch the event from the class, but in what cases would I need to use the Metadata tag?

My third question is if I write dynamic customEventClass extends Event", can I use the dynamic keyword for any purpose?


1) Redispatching event isn't the same as bubbling. Say we have some (third party) component which dispatches event without bubbling. And we want to delegate this event further. In this case we need redispatch the event:

myButton.addEventListener(MouseEvent.CLICK, myButtonClickHandler);

...

private function myButtonClickHandler(event:MouseEvent):void
{
    dispatchEvent(event);
}

In this case the line dispatchEvent(event) produces new event by cloning it.

If you're sure your components with custom events won't be used by others you can omit implementing clone(). But who knows… Best practices are best practices to follow them.

2) Event metatag solves at least two practical tasks:

  • It allows compiler to validate usage of corresponding attribute in MXML when somebody uses target component in MXML. So compiler will produce an error without Event metatag declaration as far as event attribute will be unknown.
  • It allows IDE to use code completion of available events when you're typing addEventListener in ActionScript.

3) Keyword dynamic allows to use any fields with instance even if they are not declared in class without any compiler warning (but without advantage of IDE's code completion). In event class it allows to carry any custom data with it. It is very often some event classes declares data field of Object type for this purposes with avoiding of usage of dynamic classes (like in DataEvent).


second - whats the need of metadata tag - [Event(name="modelEvent", type="com.abc.data.model.ModelEvent")] ... but In what cases i would need to specify the MetaData TAg

The metadata tag is used only for code hinting inside the IDE. If you want the event to show up in MXML code hinting of your component, you need to put an event metadata tag.

This may also effect ActionScript code hinting when you use addEventLIstener on a variable with the type of your custom class, but I haven't tested that.

In MXML, you'll get a compiler error if you try to add an event listener for an event with no metadata. ActionScript allows it, though.

I know that is only a half answer. In terms of the clone event; I recommend always creating one because I often have errors from the Flex Framework if I don't. But, I can't pinpoint a specific issue.


I use events for everything, so I find it useful to store events so that caused significant changes in the model so that after an undo, they can be dispatched again to support redo (i.e., if you dispatch the same event again, it will cause the same action again).

I don't believe that bubbling uses clone, but the source code is not available.


I hope Event Bubbling lets the same event to propagate through the container hierarchy but using clone() method makes it different by creating a new cloned instance in order to re dispatch it for where(Listener) it is listened with the new set of values of the constructor .

0

精彩评论

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