apache-flexmate

Flex, Mate: event path - what way to choose?


Imagine we have an object dispatching some event and this object should perform some action on this event. What's the right way of catching this event? May I listen to this event directly in my object or should I let this event travel through EventMap?
Example: I have a popup window with form. On submitting form, I'm going to send a request to remote server and close this popup. Is it ideologically correct to perform popup close on click without dispatching custom event to EventMap? Thanks.


Solution

  • As for me personally I prefer the object which responsible for creation of something like views or pop ups should be responsible for their destroying too. And I prefer to use Mate only for solving some cross-application tasks especially client-server communications. So I prefer using plain old ActionScript event handling on a component level. And if some component created pop up window it can listen some submit event and destroy pop up.

    Another case if pop up was created by Mate itself (I mean in event map). In this case I see no problem to destroy window in Mate handler which handles some submit event and performs request to the server and destroys window at once.

    But anyway you should dispatch Mate events (I mean events which handled by event map) using the simple rule: do not dispatch some low level events. You should convert them to events reflecting business logic on a corresponding application level. In your case you shouldn't consider submitting your form as two operations: requesting server and closing window. From window's point of view it is just form submitting and window shouldn't care if there will be server request or reading data from SharedObject or even from some kind of model. So window should dispatch submit event and Mate map should handle this event and produce request and window destroying in the same handler. This way you can use your view without knowing if view is in pop up or maybe in native AIR window. Just adjust your Mate's event map.

    And don't forget Mate's map can be hierarchical. So you can divide levels of handling event of business logic.