Anas Najem
Anas Najem

Reputation: 27

How to call a function in Init method?

I'm new to sapui5, and I'd like to know how to call a function in my Init method?

The code I have currently is below:

onInit: function() { 

        this.oInitialLoadFinishedDeferred = jQuery.Deferred();          
        var oEventBus = this.getEventBus();



        this.getView().byId("list").attachEventOnce("updateFinished", function() {
            this.oInitialLoadFinishedDeferred.resolve();
            oEventBus.publish("Master", "InitialLoadFinished", {
                oListItem: this.getView().byId("list").getItems()[0]
            });
        }, this);

        oEventBus.subscribe("Detail", "TabChanged", this.onDetailTabChanged, this);

        //on phones, we will not have to select anything in the list so we don't need to attach to events
        if (Device.system.phone) {
            return;
        }


        this.getRouter().attachRoutePatternMatched(this.onRouteMatched, this);

        oEventBus.subscribe("Detail", "Changed", this.onDetailChanged, this);
        oEventBus.subscribe("Detail", "NotFound", this.onNotFound, this);
        oEventBus.subscribe("Detail", "Cancelled", this.onDetailChangeCancelled, this);

        // this.handleSelectDialogPress();

    }   

Any suggestions?

Upvotes: 0

Views: 2029

Answers (1)

Prashob Thekkyal
Prashob Thekkyal

Reputation: 400

I think you are almost there to call function from init.

For Example, if you want to call some function which is within the same controller then use 'this' keyword (here 'this' refers to the current controller):

onInit: function() { 
   ...
   this.handleSelectDialogPress();
}

handleSelectDialogPress : function () {
  // some code
}

Refer below code if you want to call some function which is in some Utils.js

ControllerName.controller.js

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

   return Controller.extend("your.controller.ControllerName", {
      myFunc2: function(input) {
         MyUtils.myFunc(input);
      }
   });
});

MyUtils

sap.ui.define([], function() {
   "use strict";

   return {
      myFunc: function(input) {
         // some code
      }
   };
});

Upvotes: 1

Related Questions