Skip to main content
Participant
August 18, 2008
Question

Multiple instances of a View

  • August 18, 2008
  • 5 replies
  • 1188 views
Hello,

I have a view which is bound to a model (static instance as usual with Cairngorm). So far so good.

Now I would like to have a view which would contain the above one multiple times. The problem is that because the model bound to each view is static, if I modify one view, all the other ones get modified too.

To be more specific, the View in question is a Chart. And the View that contains multiple Charts is a Dashboard.

How can I workaround this ?

Regards
This topic has been closed for replies.

5 replies

Participant
October 15, 2008
Samuel,

I had a similar issue where I have multiple instances of the same View in my application.

I attacked it by creating a "DictionaryProxy" which is basically a Dictionary with Bindable events and then assigned each of my views an instance of a sub model object stored in the DictionaryProxy then bound / passed to the view. This way I can update the Model, and keep the view bound to the Model without having to pass the model to my Command / Event.

Using this method I'm able to achieve a couple of extra things I wasn't able to do using other methods.

I'm also able to Close the specific views when the item is removed from the dictionary.
I'm able to access the model from other parts of my application.

Hope this helps.

Cheers
Gareth.
Participating Frequently
August 25, 2008
Hi Samuel,

What we've successfully done in the past with this sort of MDI interface is to use instance-based instead of static models - the model is an instance variable of each view, and is passed to the command as a property of the event: the command then updates whatever model was passed to it.

You don't need to worry about not using the ModelLocator in this instance - it's fine when the app has a single model, but in this case it would mainly get in the way. Note that the model classes themselves have no dependency on the views.

Cheers,
Robin
Participating Frequently
August 19, 2008
From what you are saying it sounds like you're trying to use the same Event, and ultimately Command with each view instance? If this is the case then I would suggest you create seperate Events and Commands for each view instance. So for instance if you only have one view type and are creating many instances of the view you could use seperate events and commands for each and just inject the event which is to be dispatched into each view instance. For example something like:

view1.event=ConcreteEvent1;

Whereas the view would define an instnce member of type CairngormEvent and dispatch it accordingly but the actual concrete type would be defined at runtime. The specific Command would then update the appropriate model etc. If the view needs to pass information (data) to the Event instance then you may need to define a common base class or interface.

There are some other ways this could be done as well, most specifically with Factories or the Strategy pattern so feel free to explore them if you like.

Hope that helps.

Best,
Eric
Participant
August 19, 2008
Hi Eric,

Thanks for helping.

What you are saying makes sense. However if I've got one model per view then how do I handle my command's result handler? How would I know which model to update ?
Participating Frequently
August 19, 2008
Hey Samuel,

I think this may be more of an issue in your design then it is a Cairngorm issue as I am assuming that the views will need to bind to different data? If this is the case then you should define an abstract view (class) which contains all of the common functionality, and then separate concrete views which then bind to their respective model. If the views are all exactly the same with the only exception being the actual data they need to bind to then I would suggest you simply pass the data provider (from the model) to each instance of the view.

Hope that helps.

Eric