Skip to Content
avatar image
Former Member

How to read js file data in ui5 controller

Here is my constants.js file code , which contains the below data (static data).

var SORT_ORDER = {
    ASC : "ascending",
    DSC : "descending"
};
var YES = "yes";
var NO = "no";

i wanted to read this data in ui5 controller code.

sap.ui.define([
	'jquery.sap.global',		
	'sap/ui/core/mvc/Controller',
	'sap/ui/model/json/JSONModel',       
        "./constants"
], function (jQuery, Controller, JSONModel, constant) {
    "use strict";
    var PageController = Controller.extend("UI5Script.controller.Page1", {

        onInit: function () {
   
        var sortorder = constant.SORT_ORDER.ASC; // which throws error
},

 });
    return PageController;
});
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Oct 16, 2017 at 01:12 PM

    Changing the path doesn't helps.

    I found the solution by using the javascript "window" object

    (function (window) {
        "use strict";  
    
    
        window.appConstants = {       
            YES: "yes",
            NO: "no"   
        };
    }(window));
    

    you can access the above constant in UI5 as shown below

    appConstants.YES
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 13, 2017 at 05:58 PM


    try

    "UI5Script/constants" instead of

    "./constants"

    Add comment
    10|10000 characters needed characters exceeded