Skip to Content

Queries on UI5 controller level dependencies

Hi SAP UI5 community,

In my project structure, we have multiple controller files for multiple views. We have a common util.js file with functions to handle the common functionality for all the views. Currently in each controller, I am loading all the library dependencies using the sap.ui.define statement. Even when there are some common dependencies among all controllers, these dependencies are present in each of the controller.js files (in sap.ui.define).

1) Since the views are loaded one after the other, Does UI5 internally take care of loading those dependencies declared in multiple controllers only once?

say first controller.js has - sap.ui.define([ "sap/ui/core/mvc/Controller", "sap/apimgmt/ui/analyticsNextGen/utils/vizFrame","sap/m/Table")]....

second controller.js also has - sap.ui.define([ "sap/ui/core/mvc/Controller", "sap/m/Table")]....

Is the sap.m.Table loaded in UI5 only once? When UI5 encounters it for the second time in second controller.js, does it return the library export value alone (without loading it again)?

2) In the common util.js file we have, I will still have to use UI5 libraries to create common SAP UI5 tables etc. Hence I am using sap.ui.require to get the instances of already loaded exports to work with the same -

var Table = sap.ui.require("sap/m/Table");

var oTable = new Table({});

Is this a correct approach of doing the same?

Or is there a way in which I could use the sap.ui.define statement to load the libraries in this common util.js file which is not a controller? (Note that the functions in this util.js file would be called from the controller files)

My intention is to load the libraries only once so as to make my application performance efficient.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Mar 17 at 03:27 PM

    1. Yes, it is loaded once

    2. Why not use `sap.ui.define` insteads of sap.ui.require, so that we are consistent in loading libraries?

    Maybe you can provide a small snippet on why you cannot use sap.ui.define

    thanks

    Dennis

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Dennis,

      Here's the code snippet as requested -

      controller.js -
      sap.ui.define([ "sap/ui/core/mvc/Controller", "sap/m/Table","util"], function(Controller, Table, util){
      "use strict"; 
      return Controller.extend("controller.Overview", {
      refresh : function(oEvent){
      createTable();
      }
      });
      util.js -(contains various functions as shown below which are called from controller.js)
      var createTable = function(){
      var Table = sap.ui.require("sap/m/Table");//returns the export of already loaded library from controller.
      ---
      return Table;
      }
      var func2 = function(){
      }
  • Mar 18 at 02:43 PM
    sap.ui.define(["sap/m/Table"], function(Table){
    
    "use strict"; 
    
    return {
    createTable:function(){},
    
    function2:function(){}
    
    
    
    
    };
    
    }
    
    
    
    );
    Add comment
    10|10000 characters needed characters exceeded

    • how you call that function in your controller?

      sap.ui.define(["sap/ui/core/mvc/Controller", "sap/m/Table","YOURNAMESPACE/util"]
      
      make sure you reference your util correctly. then util.createTable()
  • Mar 19 at 04:54 AM

    util.js can be written as

    sap.ui.define("sap/m/Table", function(Table) {
      return {
        createTable: function() {
          var Table = new Table();
          ...
          return Table;
        },
        func2: function () {
           ...
        }
      };
    });
    
    Add comment
    10|10000 characters needed characters exceeded