App controller - do I need to split mine up?


#1

Hi everyone,

I feel like I’ve got a pretty good handle on things, but as I’m building out my first (larger size) app, I’m starting to be concerned with the size of my controller.

Currently, I’m using 3 tabs in the main app, plus a handful of ionicModals to add a few extra menus to the app.

Due to the fact that the modals can’t easily be passed controllers, I’ve included them under the main app controller (AppCtrl). I’ve also started to put the controller base for the three tabs into AppCtrl (this is probably temporary, the tabs will most likely end up with their own controllers: Tab1Ctrl etc).

My question is: how necessary is it to split up AppCtrl? Will it effect performance.

Any other thoughts are welcome too.

Thanks, David


#2

Hey David,

Assigning controllers to modals is actually as easy as including a ng-controller tag to the top level of the modal html. For example, for the below:

    <ion-modal-view ng-controller="modalController">
        <ion-header-bar>
          <h1 class="title">My Modal title</h1>
        </ion-header-bar>
        <ion-content>
          Hello!
        </ion-content>
   </ion-modal-view>

You can then split out the controller code into js/controllers/modal.controller.js and include it in your index.html.

I hope that helps.


#3

Of course! So simple.

Will larger controllers effect performance? I have a fairly vague understanding; is there any other reason to split up the controller other than readability?


#4

I wouldn’t think so, in the end it all compiles down, though if things need to bubble up the chain to find the scope, perhaps there would be a tiny effect. I suspect others who know more about the JavaScript compiling process would be better equipped to answer that one.

But definitely will effect maintainability and debugging, which encourages cleaner code, which encourages more streamlined code, which encourages better performance :smile:


#5

That sounds like more or less what I was thinking. Thanks.


#6

You always should try to avoid large methods or files. Split them up, create services when needed, per-controller files etc. This is not for performance, more for organization.