2

When building an application what is the best way to setup your controllers?

I understand that routing, event listeners, and most all interactivity should be managed by the controllers, but my main controller is starting to grow out of control and I'm not sure how to best separate my logic into separate controllers without keeping them all "running" all the time...

Spencer Alger
  • 918
  • 2
  • 9
  • 22

2 Answers2

2

It's okay to have them all loaded at app start-up, even with hundreds thousands of controllers. It's the view rendering that takes time.

Make sure your app is minimized and concatenated though, using sencha cmd.

In a test, I created 1000 simple controllers, like this:

Ext.define('app.controller.ControllerN', {
    extend: 'Ext.app.Controller',
    init: function(application) {    
        this.control({
            buttonN1: {
                click: function() {
                }
            },
            buttonN2: {
                click:function(){}
            }, 
            ... 20 listeners
        });
    }
});

I concatenated them into one file, and loaded them in app.js like this:

Ext.application({
    name:'app',
    controllers:[
      'Controller0',
      'Controller1'
       ... 1000 controllers
    ],
    launch:function(){}
}

It took ONE second from browser refresh (Chrome) until the last controller's init method was called.

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • 2
    How about writing 1000 controllers that listen 20 events each? It won't be as fast as you would think. It's true that usually rendering gives you the bottleneck, but the DeftJS way works nice. In DeftJs you have viewcontrollers tied up to the view instance. In ExtJS controllers are singletons. – catalinux Jun 09 '13 at 15:35
  • @catalinux. Updated my answer. Singleton controllers work fine in extjs, even with multiple view instances. How do you select all the fields in a form with Deft? – Neil McGuigan Jun 25 '13 at 21:21
1

I had similar problem so I divided controllers on the basis of business functionality it will support, e.g. userController does all the user related operations like login, logout, update etc whereas cartController does all the operations related to shopping cart like add to cart, apply coupons, payments etc. Since a single view can have many functionalities related to different areas of app so you can add refs to this view in multiple controllers and listen to only relevant events in corresponding controller.

ThinkFloyd
  • 4,981
  • 6
  • 36
  • 56
  • Okay, do you just add all of your controllers to your main application? Do you know how to define which controllers you need based on the active views? – Spencer Alger May 10 '13 at 19:00
  • Yes I do add all the controllers in `Ext.application` `controllers` array so that app loads all of them and find out which controller will listen to fired event based on controller's `control` config. BTW I did not understand your question : `Do you know how to define which controllers you need based on the active views?` because controller's `ref` config is where we define which views are handled by the controller. – ThinkFloyd May 13 '13 at 05:39