cancel
Showing results for 
Search instead for 
Did you mean: 

Queries on UI5 controller level dependencies

0 Kudos

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.

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member182862
Active Contributor

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

0 Kudos

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(){
}
former_member182862
Active Contributor
0 Kudos

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 () {
       ...
    }
  };
});

0 Kudos

Hi Dennis,

I have tried this. But when I am calling the function from controller.js, it is unable to find the function present in util.js. Please note that I am defining this file util.js for loading as a controller dependency as well.

former_member182862
Active Contributor
0 Kudos

I am not sure how the files are layout. How does the folders and files structure looks like?

junwu
Active Contributor
0 Kudos
sap.ui.define(["sap/m/Table"], function(Table){

"use strict"; 

return {
createTable:function(){},

function2:function(){}




};

}



);
0 Kudos

Hi Jun Wu,

I have tried this. But when I am calling the function from controller.js, it is unable to find the function. Please note that I am defining this file util.js for loading as a controller dependency as well.

junwu
Active Contributor
0 Kudos

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()