4 Replies Latest reply on Jul 25, 2010 1:09 AM by liorchaga

    Multiple instances of same view in MVC flow

    liorchaga Level 1

      Hey,

       

      I need an advice on proper MVC design.

      I have a tab navigator, that each view in it's stack is actually the same component that gets a different filter.

      Each of these view are included in an MVC flow. i.e. when the view is loaded, it calls the PM with it's filter ID.

      The PM dispatches an event, the command intercepts it, calls a service and the updated the PM data. The view, of course, is bound to the PM data.

       

      This is the context:

      
      <mx:Object xmlns="http://www.spicefactory.org/parsley" xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:data="com.adobe.cairngorm.integration.data.*"
            xmlns:module="com.adobe.cairngorm.module.*"  xmlns:MVC="MVC.*">
           <MVC:ServiceStub id="service"/>
           <MVC:PresentationModel id="pm"/>
           <MVC:SomeCommand/>
      </mx:Object>
      
       
       
      

       

      In my view (which is instantiated several times), I inject the pm in the following manner:

           [Bindable]
           [Inject(id="pm")]
           public var pm:PresentationModel
      

       

      Obviously, you can see where the problem is - all view instances are working with the same PM.

      This would not be so problematic if each time I select a view in the tab navigator, it would invoke the entire MVC flow. However, this flow is invoked only when the view is loaded first time (and it should be invoked again only if the user explicitly wants to refresh data).

      You can see a simplified example (with source) here, notice the behavior when I select a view, select another view and then come back to the first view. I expect the application to change the data in the combo accordingly, but obviously in the current design it is impossible.

       

      I should state that I would not want to declare the PM (and the command) several times in the context, because ultimately I would want the number of instantiated view to be dynamic.

       

      1. Is there a way for the PM to be instantiated for each view instance independently?

      2. If so, this would require instantiating many commands as well, each one working with a single PM instance. How can I make sure only the command corresponding to a PM is intercepting the event dispatched by it?

       

      My only solution, for now (and maybe it is the right one), is to hold in a single PM the data for all views in some kind of a dynamic object, and then in the view, bind the combo to data[filterID] instead of to data.

       

      Please, advise me on this one.

      Thanks,

      Lior.