Skip to Content
Author's profile photo Srikar Nagadevara

Extend from Super Class in a view-controller.js (Inheritance)

Hello Everyone,

Now we can see an interesting topic Extending the controller.js file from a Super Class .

Technically called Inheritance.

Basically we all know that how to create a Sample UI5 Application from a template. While creating it from template we automatically get a single view and controller created.

This is how they look like after the code generation.

While opening the my.View.controller,js

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

	return Controller.extend("com.open.controller.myView", {

	});

});

If you closely observe the controller the view’s controller has got extended

But we can also extend a different file which considered as a super class and we can access the methods from the super class. Now let us create a Super Class. In Order to Create a Super Class which is a JS file, following are the steps needed.

Give as CustomClass.js 

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

		return Controller.extend("com.open.CustomClass", {
			getModel : function (sName) {
				return this.getView().getModel(sName);
			},
                       	setModel : function (oModel, sName) {
				return this.getView().setModel(oModel, sName);
			},
                        getResourceBundle : function () {
				 this.getOwnerComponent().getModel("i18n").getResourceBundle();
			},
		});

	}
);

Using the above Custom class now go to the myView.controller.View  and do as below

Remove and Add the below code.

Code :

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

	return CustomClass.extend("com.open.App", {

		onPress : function () {
			var that = this ;
			var ModelData = that.getModel();
		}
	});

});

 

Re-usability : 

Therefore we can access all the methods declared in the custom class and we can extend this custom class to any view . This is how we can re-use the code for all the controllers. In-fact write the code only once and access many times from the controller file.

Thank You, Reward if helpfull

Cheers…

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Klaus Kronawetter
      Klaus Kronawetter

      Great post!

      One question though: Is it also possible to reuse such a generic "Super-Controller" for more than one application?

      BR, Klaus

      Author's profile photo Srikar Nagadevara
      Srikar Nagadevara
      Blog Post Author

      Hey Thank You Klaus,

      Want I understood from your question is, yes we can reuse - extend the Super Class for more than one controller in your Application. For Example, if we have 3 views in your Application then it is possible to extend the super class for all the 3 controllers .

      Since Application to Application differs we can also extend the same super class in all your controllers of other applications.

      Please reply if I got you wrong..

      Author's profile photo Anil Paladugu
      Anil Paladugu

      Good explanation Srikar.

      Klaus,

      For more examples just create SAP Ui5 application through WEB IDE and you can find  BaseController.js file which is extended in remaining controllers. In this example BaseController file has Routing logic to navigating between views.

      Author's profile photo Former Member
      Former Member

      Hi Srikar,

      Good post, very helpful.

      Ravikiran