Is this an ok approach (how I'm notifying another view when Tree is changed) - plus general dispatch question
Pardon if this is very basic (pretty new to Flex/AS/RIA development - experienced in the web java world.)...
I have a Tree on a Panel and when any part of the Tree changes (click event, whatever) a different view needs to be notified. I'm currently handling it like this:
//TreePanel view mxml <mx:Tree id="myTreeNav" ....
change="dispatchEvent(new PromptTreeEvent(PromptTreeEvent.TREE_CHANGED, Tree(event.target).selectedItem as XML))"
//in TreePanel mediator: //on register
eventMap.mapListener(treePanel, PromptTreeEvent.TREE_CHANGED,
onPromptTreeChange);
//handler private function
onPromptTreeChange(event:PromptTreeEvent):void {
dispatch(new PromptTreeEvent(PromptTreeEvent.TREE_CHANGED, event.xmlNode));
}
Couple questions...
1) Is the above approach ok? It seems sort of odd to just have my mediator acting as a proxy in a sense to just turn around and dispatch the same event? By default it doesn't look like it's easy to dispatch a Framework event directly from the view (although from another forum post here I heard it's possible if you really wanted.)
2) is there a reason why I can't turn around and dispatch the
same event in my handler instead of having to make an identical new
instance of the Event? I thought i'd just be able to do the
following (but it didn't work):
//in mediator handler private function
onPromptTreeChange(event:PromptTreeEvent):void {
dispatch(event);
}
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by Shaun Smith on 14 Feb, 2010 07:42 PM
In order to re-dispatch an event in AS3 you have to override the Event's clone() method. Once you've done that, you can easily re-dispatch in your Mediator like so:
3 Posted by squeedee on 15 Feb, 2010 09:09 PM
I'll reitterate what I mentioned in IRC for the benefit of all.
My style preference here is not to hand view-component events around on the bus, but in fact to 'decode' the intent.
Assume: We have MyTreeView and MyTreeMediator
If clicking the tree 'Selects a group' or 'Selects an item' or 'Begins an edit' within the system, I would do the following:
I know this sounds verbose, but you're way ahead of the game if you follow this approach. You are now modelling IN your domain model, not your views.
4 Posted by squeedee on 15 Feb, 2010 11:05 PM
Rick, In IRC you asked:
Please be aware that this is not how actionscript works. the following code:
Is completely synchronous. All Handlers of EventA.THING will be run, synchronously, before any handler for EventB. There is no threading here.
Event's are an easy way to handle asynchronous actions. They are not themselves asynchronous.
Till Schneidereit closed this discussion on 02 Mar, 2010 12:46 PM.