2

I was wondering how to go about splitting up a controller in multiple JavaScript-files. I currently have a controller which has about 5000+ lines and it is rather frustrating to navigate through it. Any suggestions how to get started?

Matthijs Mennens
  • 1,125
  • 9
  • 33

3 Answers3

5

You can start with several approaches:

  1. Use "Mixin" pattern
  2. Split your logic up to several "classes" and put them to separate AMD modules, then just "require" them and use in your controller

Mixin can be implemented as an AMD module which just returns JSON object with all needed functions. In order to inject it to the controller, just make use of jQuery.extend method to enhance the controller's object with mixin.

Andrew Naumovich
  • 1,441
  • 1
  • 12
  • 15
2

This question would lead to somewhat opinionated answers but generally, you could start off by:

I'd also encourage to write some regression tests before you start.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
0

Another approach is to set up some hierarchy between your controllers and use inheritance between the controllers. Move the logic which is used by several controllers to one BaseController.js.

For example you can set up a BaseController.js which extend from sap/ui/core/mvc/Controller:

sap.ui.define([ "sap/ui/core/mvc/Controller" ], function(Controller) {
    "use strict";

    return Controller.extend("BaseController", {

        someBaseFunction : function() {
            // Do something
        },

    });

});

And the in another controller e.g. App.controller.js extend from your BaseController.js:

sap.ui.define(["%PATH_TO_YOUR_BASE_CONTROLLER%/BaseController",

], function(BaseController) {
    "use strict";

    return BaseController.extend("%PATH_TO_YOUR_APP", {

        onInit : function() {
            // Call function from BaseController.js
            this.someFunction();
        }

    });

});
Mike
  • 14,010
  • 29
  • 101
  • 161
Francesco Iannazzo
  • 596
  • 2
  • 11
  • 31
  • I have 4 controllers for 1 view and I am currently using following solution. My Main controller is extending subcontroller3, subcontroller3 is extending subcontroller2 and subcontroller2 is then extending subcontroller1. This works, but it doesn't seem like a proper solution. Can I somehow extend the main controller to all 3 subcontrollers? – Matthijs Mennens Jan 05 '18 at 09:51
  • No, I think this is not possible. One ui5 Control can only extend always one other control. So the approach you have chosen is the proper solution to do that.. – Francesco Iannazzo Jan 05 '18 at 10:05
  • 1
    What you can try is to extend controllers prototype with all your subcontrollers. Anyways Inheritance is not neccessarily the best way to organize code... I found that esp. with deep inheritance chains it can make things overly complex. You might be better off with a mixin approach. – cschuff Jan 05 '18 at 10:08
  • @cschuff That's right but the Ui5 framework does not offer a good way of splitting code. Splitting the code into several classes and then require the code inline can also construct pitfalls, because you never know which class / controllers requires your code. In big projects this also cause problems. – Francesco Iannazzo Jan 05 '18 at 10:10
  • @FrancescoIannazzo JS itself does offer possibilities to split up code. Check this article on mixins for example: https://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/ – cschuff Jan 05 '18 at 10:19
  • @cschuff Thanks for the article , I will go through it. Looks interesting. – Francesco Iannazzo Jan 05 '18 at 10:26