Multiple View Instances & Services

Bruce's Avatar

Bruce

14 Sep, 2012 12:14 PM

Assumptions:
1. I have a view, DataView
2. I have a view mediator, DataViewMediator
3. I have a service, DataService
4. My application consists of 10 instances of DataView

So, in my DataContext I have

mediatorMap.mapView(DataView, DataViewMediator);
injector.mapSingletonOf(IDataService, DataService);

commandMap.mapEvent(ContextEvent.STARTUP, StartUpCommand, ContextEvent, false);

The StartUpCommand will execute a method in DataService 10 times with 10 different URL parameters. I add an "ID" to each AsyncToken so that in the response handler I know what the calling URL parameters were. I would like the data in ResultEvent to be mapped to a specific view. How do I go about this? What if I have a model sitting in between the service and the view?

I am trying to think of what might be the best method of wiring this up.

  1. Without using a model, I suppose I could configure each view with an "ID". The service could then broadcast an event, all 10 instances of the view mediator would be listening for the event, but only the mediator that could match the ID in the event payload with the ID of the view instance would end up setting the dataProvider on the view, ie

    if(event.ID == view.ID)

      view.dataProvider = event.data;
    

Would this be the preferred method? Is there a way to make this work without having to have each DataMediator do matching logic?

  1. What if I want a model to sit between the service and the view/mediator? It would seem to me I would need to make the following changes

  2. I think I would need to wrap up the instances of DataModel inside a singleton model, say WrapperModel. Then I could write methods on the singleton model to access instances of DataModel

  3. The DataService response could check to see if an instance of DataModel exists inside WrapperModel. If it it does, it overwrites it. If it does not, it creates it. It then fires an event that the model has been updated.
  4. DataViewMediator would respond to model events. It would check for existence of a DataModel instance existing inside WrapperModel. If it finds a DataModel with the correct ID, it maps the data to the DataView.dataProvider

Does this sound reasonable? Is there a way to use DataModel without wrapping it up in a singleton model?

  1. Support Staff 1 Posted by Ondina D.F. on 14 Sep, 2012 02:18 PM

    Ondina D.F.'s Avatar

    How about having a dictionary, or whatever suits you, of views’ ids mapped to different event types corresponding to the constants specified in a custom event class? The Service (or a Command) gets the event type from the dictionary (stored in a VO or Model), and dispatches the custom event mapped to the view’s id (added to the AsyncToken), so only the mediator of that view would react to that event.

    Just a spontaneous idea.
    I haven't really put much thought into it yet, due to lack of time, so, feel free to criticize the potential pitfalls of that approach.

  2. Ondina D.F. closed this discussion on 20 Sep, 2012 02:49 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac