How to extend a sap.ui.base.ManagedObject


The sap.ui.base.ManagedObject offers many functionality out of the box

  • state management
  • data binding
  • managed properties
  • managed events

Template

sap.ui.define([
	"sap/ui/base/ManagedObject",
	"sap/ui/model/resource/ResourceModel",
	"sap/m/MessageBox"
], function(UI5ManagedObject, ResourceModel, MessageBox) {
	"use strict";

	return UI5ManagedObject.extend("<PROJECT_PATH>.control.<NAME>", {
		metadata: {
			properties: {
				propertyA: {
					type: "string"
				}
			},
			events: {
				eventA: {
					parameters: {
						eventParameterA: {
							type: 'string'
						}
					}
				}
			}
		},
		/**
		 * @class <NAME>
		 * @summary <SUMMARY>
		 * @extends sap.ui.base.ManagedObject
		 */
		constructor: function(sPropertyA) {
			UI5ManagedObject.apply(this);
			this.setPropertyA(sPropertyA);

			//Just an example to show that you can define a specific resourcemodel for the ManagedObject
			// this._oi18nModel = new ResourceModel({
			// 	bundleName: "<PATH>.i18n.i18n",
			// 	supportedLocales: ["en"]
			// });
			// this.setModel(this._oi18nModel, "i18n");
		},
		/** 
		 * @memberOf <NAME>
		 * @description example of how to fire a event with parameters
		 */
		showMessageBox: function() {
			MessageBox.confirm("Lorem Ipsum", { //this._oi18nModel.getText("xyz")
				onClose: function(sAction) {
					this.fireEventA({
						eventParameterA: sAction
					});
				}.bind(this)
			});
		}
	});
});

Example

var oManagedObject = new ManagedObject("Lorem");
oManagedObject.getPropertyA();
//Expected output: 'Lorem'
oManagedObject.showMessageBox();
oManagedObject.attachEventOnce("eventA", function(oEvt) {
	oEvt.getParameter("eventParameterA");
	//Expected output: 'OK' or 'CANCEL'
}.bind(this));
// oManagedObject.attachEventA(function(oEvt) {
// 	oEvt.getParameter("eventParameterA");
// 	//Expected output: 'OK' or 'CANCEL'
// });

Leave a Reply

Your email address will not be published. Required fields are marked *