cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Abstract Class - UI5 & javascript understanding

emil_lazarski
Explorer
1,365

Hi Guys,

I am trying to understand & learn some concepts of UI5 and I was going through some tutorials but rather than copying thoughlessly code from tutorials I prefer understanding what is happening below the surface.

One of the examples I encountered was handling the class sap.m.image event press. According to the API reference event is fired, which I handled in a View Controller in a line:

onImagePress: function(oEvent){
            var oSOurce = oEvent.getSource(); 
            var ID = oSOurce.getId();
}

I wanted to get the Source and finally get the ID of the image.

Here unfortunatelly comes my question. According to API reference, getSource of oEvent returns object of class EventProvider:

Although the class is marked in API to be abstract it can be instantiated (according to API ref., which is in contradiction to my understanding of abstract classes).

I was trying to understand where does the availability of method getId come from and tried to debug the Event.js but without any spectacular results. I was hoping that maybe methods like oSOurce.constructor.name could help but not really.

 Event.prototype.getSource = function() {
		return this.oSource;
	}; 

Accepted Solutions (1)

Accepted Solutions (1)

FlorianVogt
Product and Topic Expert
Product and Topic Expert

Hi Emil,
these are many interesting questions in your post. Let me try to answer them:

1. Abstract Class - General
JS (ES5) has not a concept of abstract classes. So, UI5 just use the term "abstract" as a logical concept and to inform the developer to not instantiate this class. The functionality of a class might be missing when the class is instantiated by the developer itself. UI5 internally there might be usages of the instantiation but for application developers this is not useful.


2. Constructor of abstract classes in Documentation
The constructor is public available because there might be subclasses extending the abstract class and these classes have to know how they can call the super class constructor. However having this code snippet in an abstract class constructor is wrong. This we will fix.

3. getId - Single Point of truth
In JS it's not possible to determine which class has introduced a method e.g. getId. You can only determine the class an instance have which has the method e.g. getId. The getId method is placed on the sap/ui/base/ManagedObject but only subclasses, like sap/ui/core/Element or sap/ui/core/Component does enforce the IDs. For further information see https://ui5.sap.com/#/api/sap.ui.base.ManagedObject%23methods/getId

emil_lazarski
Explorer
0 Likes

Thanks for the fix and explanation.

The only thing I did not get is:

You can only determine the class an instance have which has the method e.g. getId.

FlorianVogt
Product and Topic Expert
Product and Topic Expert
0 Likes

e.g If you have an instance of sap/m/Text you can check that getId is available and you can determine that the instance is based on the class sap/m/Text but not which class in the class hierarchy has introduced getId.

Answers (1)

Answers (1)

WouterLemaire
SAP Mentor
SAP Mentor

Try to use TypeScript for your ui5 app. This has the concept of real classes including abstract classes. Here you have the getting started