Question

We have an application wich has a typical Master/Detail view. The master view displays a list of ticket titles and if you click on a ticket title you see a view with the ticket details.

The master view has a model wich is a list of "ticket" objects each containing more information about a specific ticket. We bound a handler on click which basically instantiates a new detail view and displays it. This works fine, except that we have to give all the config values as primitive data types to the constructor (we know we can't use objects when instantiating)

We thought about another way to do this but couldn't get it working. In the Init() (of the detail view model) we only pass the ID (of the clicked ticket) and wanted to use Mvx.Resolve to get the master view model. So question one would be, can you fetch arbitrary view models inside other view models?

A second idea would be write a base view model class from which all view models in our app inherit, which registers the view model on instantiation to a service so that you can call that service and fetch the view model from anywhere (mediator pattern). Would that be against the framework?

An answer that I have found is "use messages" but to me it seems sort of bulky, at least how I understood this. The detail view model would have to send a message to the master view model "Ok I'm ready now" and then the master view model would say "Alright, heres the configuration".

So to sum up the questions:

  1. Is it possible to fetch certain view models from the framework inside other view models?
  2. Would implementing this by hand be against the framework?
  3. Did we understand the message approach correctly and if no, what would be a more lightweight/generic way of doing it?

Cheers and thanks

Tom

Was it helpful?

Solution

Usually when I have to share data across View Models, I use a Service to manage it. The service is typically injected via IoC.

I treat View Models as a mediator between the Services and the View. Most of my logic is stored in the Service. The only things I have in the View Model are properties for data binding, commands that dispatch to a service, and other presentation level concerns.

For your scenario, I would have the MasterViewModel pass an identifier to the DetailViewModel, where it will ask the ConfigurationService for the configuration by id.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top