ExtJS 4 MVC multiple instances of views and sub/child controller difficulties

11,356

Solution 1

I was faced with a similar problem:

Consider a tabpanel for a CRM type application which opens new instances of a view for each client. And say that tab view contains 3 or 4 row-editing gridpanels for interacting with different collections of data relating to that client.

The solution I came up with was based on this from the Sencha forums. In a nut shell, almost all events that are dispatched from a view contain a reference to the view itself. The handlers in my controller's control function all use these to get a reference to the correct view instance.

For dealing with the multiple instances of the same store needed for this, I took this to heart from that post:

For the Store instance on the view or a global one... depends on the needs. If you are going to use globally then make it global. If you only are going to need it on the view then put it on the view. MVC is not a law, you can change it to fit your needs. Technically the Controller part of MVC is suppose to be the middle man between the View and Model parts but sometimes that's just not needed. I create the Store in the view 95% of the time. I'll give you an example...

If you have a Store for products, you probably only need to reference that Store in your Grid. That usually isn't needed for other parts of the application. However, if you have a Store to load countries, I often need it globally so I only have to load it once and can then set/use that Store in several views.

So I just created the needed store's that relate to a view instance specifically, inside the view's initComponent method. The application did have a few global stores that I created as store classes following the MVC recommendations. It worked out nicely to encapsulate the view instance stores inside the view. Then I only needed one instance of the controller.

To answer your question specifically, currently, there is no ExtJS official recommendation or config for dealing with multiple instances of the same view that use the same store constructor. I have spent some time looking for something like that and the best I have found was this recommendation from one of their forum moderators.

Solution 2

I don't think you ever need more than 1 instance of a controller, regardless of how many views/models you have. See functional example here:

http://whatisextjs.com/extjs-4-extension/fieldset-w-dynamic-controls-7

Solution 3

This can be done, reasonably easily. You need to follow a few rules:

  1. load your controllers at app startup. Don't unload them. Don't worry about the memory or time, it's pretty small even for hundreds of controllers, as long as you minimize and concatenate your js.

  2. Never use the refs or views properties of a controller. You are going to use one instance of a controller, but multiple instances of views, so you never want a reference to a view.

  3. only use event listeners in controllers. You are only going to listen to events on your views. You can always get a (temporary) reference to a view in the event handler via the "cmp" parameter in the handler.

  4. To "launch" a view, create it and add it to another view. To destroy it, destroy it. You don't use a controller to launch a view. You can use the afterrender and beforedestroy events in the controller to add logic.

Solution 4

In ExtJS' MVC the controller is a Singleton for you view. I like how DeftJS thinks about MVC. Each instance of a view has an own instance of a controller. In this way you can put all "controlling rules" in a controller for a particular part of your view, and this will be instantiated only when the view opens.

I did not have any experience how I could use multiple Defts JS apps in the same project.

Share:
11,356

Related videos on Youtube

aenigmatic
Author by

aenigmatic

I am an application developer and software designer. I use fun script languages like PHP, Javascript, Bash, etc.. as well as many frameworks and plugins. I do not think jQuery is the best thing to appear since the interwebs became cool. Just have to put that out there. o_o

Updated on June 04, 2022

Comments

  • aenigmatic
    aenigmatic about 2 years

    I have encountered a problem with the MVC pattern in ExtJS 4. At least, I think I have. Having approached multiple people with this question, and having posted numerous times in the Sencha forums, I am now turning to a broader audience in hopes of getting either a light bulb or a confirmation.

    Problem

    Your application has the ability to open many different views, some of which themselves are mini-applications. Additionally, a user may wish to have multiple concurrent copies of a view open.

    This application is a single-page client-side Javascript application.

    The ExtJS 4 MVC model expects you to define all of your controllers in your Application class. These controllers are then initialized when the Application loads. Controllers keep track of views, models and stores.

    When you initialize controller A multiple times, say to create more than one copy of a view, you end up with two views that reference the same data stores, and functionally send duplicate events to the Application event bus.

    I have refactored my application by adding new prototype methods to Component and Controller to allow for both a) sub controllers (some of my controllers were getting pretty huge) and b) defining stores specifically for the view they work with. The models can still be defined on the controller, just for ease of use by handlers if you need to do something like grab a record from the server.

    Question

    My understanding of MVC would lead me to believe that models more directly relate to the View than then Controller. I asssssume that ExtJS 4 decides to attach stores (which I think can be seen as wrappers to a more classic model) to Controllers for purposes of encouraging re-use of loaded data, and to optimize away from having many copies of the same class instantiated. It seems to me, however, that one cannot do this if one intends to have many instances of a view available to the user. To my thinking, having many instances is an important option in an OO framework, hence why I have bucked the trend and implemented prototypes on some of the Ext base classes. (Thank you Ext.implement!).

    Is there any way to have multiple concurrent instances of a view with different data loaded into them using the out of the box MVC classes and making uses of the provided setters, getters, etc?

    • casperOne
      casperOne over 12 years
      So what exactly is the question here? I don't see a clear, coherent question in the Question section. Please update your question to include a question that can be answered with facts, references, (not subjectively, with opinions, etc).
  • aenigmatic
    aenigmatic over 12 years
    I think you are missing the point. I need multiple instances of a view, not of a controller.
  • aenigmatic
    aenigmatic over 12 years
    this is fine, but it isn't really functioning within the MVC package. This also loads a single record into a view. I am referring to creating multiple view instances with attached stores.
  • dbrin
    dbrin over 12 years
    I don't understand what you are asking here. Can you use Ext.create('MyView') multiple times? Yes. What's the problem?
  • Oleg
    Oleg almost 12 years
    I needed a multiple instances of controller and you answer is really correct!
  • Dave
    Dave over 10 years
    So far, so good. But what about the unique copies of the stores on the created views?