Sending and receiving messages
Rather than reaching into View
, we can have ViewModel
signal View
when it is time to display the dialog or other View
-dependent element.
For example, suppose we want to show Snackbar
when the user clicks on the Create button. Rather than using an event handler, we can use Command
(which is preferred because it puts the logic into ViewModel
). ViewModel
might then massage data or otherwise do whatever it needs to do, and then signal View
to display Snackbar
by sending out a message to that effect.
The idea is that ViewModel
publishes a message such as “anyone who has subscribed to this message, show a Snackbar
” and the page subscribes to that message and so shows Snackbar
when the message is received.
In some circumstances, there may be more than one Subscriber. For that matter, in some circumstances, more than one Publisher can send the same message, as shown in Figure 5.3:
Figure 5.3 – Publish and Subscribe...