Skip to Content
Author's profile photo Srikar Nagadevara

Call Custom methods in your Controller

Hi All, After going through the hana on demand i got to know that we can add the custom methods from a js file to our controller.

Few weeks ago i had a requirement that i need to call the methods from a different file . So i have done this using sap.ui.require  & sap.ui.define

Create a SAP UI5 Application and add a view and controller to it .

Add a module file with our custom methods to the application.

Module.js:

sap.ui.define(["./Module"], function() {
    return {
        customMethod : function() {
            var strin = "My Module data";
            return strin;
        }
    };
});

using sap.ui.define we can define a file and add as many as custom methods.

View.controller :

Go to the controller and if we want to call a method from the module.js file we need to write the below code as shown below

sap.ui.require(["view/ModuleA"], function(ModuleA) {
				var textset = ModuleA.customMethod();
				console.log(textset);
			});

Include above code into your controller code and call the methods now .This is how you can call the module data .

Similarly, We can also do the below method to include files in our controller.

sap.ui.define([
	"com/open/controller/BaseController",
	"sap/ui/model/json/JSONModel",
         "com/ext/controller/Module"
], function(BaseController, JSONModel,Module) {
	"use strict";

	return BaseController.extend("com.open.controller.App", {

		onInit: function() {
                var data = Module.customMethod();
}
	});

});

Using this code we can call any method in the module.js file by include it in module libraries at the top. So this is how we can add the custom methods and include the files in the application.

Cheer’s…

 

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Good job! Keep it up!

      192.168.0.1