handle an action and make it continue bubbling.

Topics: Actions & Coroutines
Sep 19, 2013 at 3:19 PM

Is there any way to handle an action in the current viewmodel, and then allow them to continue bubbling up in the visual tree looking for a second target?

Sep 20, 2013 at 9:10 AM
CM actions use a specific target and a method, so the concept of 'bubbling' makes no sense, in my opinion.
Can you describe your scenario? Maybe you can navigate the VM tree (if any) to propagate the action?
Sep 20, 2013 at 10:13 AM
Edited Sep 20, 2013 at 10:57 AM
Thank you very much for your interest BladeWise.

This this my simplified scenario

I have a view, lets call it MainView (a window) and his viewmodel MainViewModel.
Inside the MainView I have nother view (an UserControl) lets call it ChildView, and his ChildViewModel.
In ChildView I have a button with an action HelloWord that displays a messagebox.

Given this scenario, I can create a public method HelloWord that handles the action, and I have tested 3 situations.

1.- I can put HelloWord method in ChildViewModel and it will show the message.
2.- I can put HelloWord method in MainViewModel and it will show the message. As there is not a HelloWord method in ChildViewModel, the action "bubbles" to the MainViewModel and fin a HelloWord method that is executed. (As described in the documentation section "All About Actions" in the subsection "Action Bubbling")
3.- I can put two HelloWord methods both in ChildViewModel and in MainViewModel, but then only the method in ChildViewModel will be executed.

I want to put HelloWord methods both in ChildViewModel and in MainViewModel, and I want that the one in ChildViewModel to be executed first, and then
the one in MainViewModel to be executed.

¿Is this posible?
Currently I use the 1sr option, then in ChildViewModel I publish an event using the EventAgreggator that the MainViewModel handles and then it executed his method.

I wonder if maybe there is another way: Let the action bubbling to the MainViewModel after ChildViewModel handles it.
It Is possible?

Thanks Again.
Sep 23, 2013 at 7:39 AM
I think that the EventAggregator approach is probably fine.
Anyway, depending on the actual scenario, you could even define proper interfaces for Main and Child view-models, and invoke Main method directly inside of the Child's one.