Skip to content

Instantly share code, notes, and snippets.

@uladzislau-stuk
Last active May 13, 2019 13:14
Show Gist options
  • Save uladzislau-stuk/cba77e2a4263fd6d1a6ce516a430d593 to your computer and use it in GitHub Desktop.
Save uladzislau-stuk/cba77e2a4263fd6d1a6ce516a430d593 to your computer and use it in GitHub Desktop.
[Base controller] Sap reusable methods #sap #sapfiori
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/m/MessageBox",
"sap/ui/core/routing/History",
"sap/base/Log"
], function () {
return Controller.extend("com.sap.fiori.BaseController", {
/**
* Convenience method for accessing the router.
* @memberof com.sap.fiori.controller.Base
* @return {sap.ui.core.routing.Router} the router for this component
* @public
*/
getRouter: function () {
return sap.ui.core.UIComponent.getRouterFor(this);
},
/**
* Getter for the event source property
* @memberof com.sap.fiori.controller.Base
* @param {sap.ui.base.Event} oEvent - the event object
* @param {string} sPath - the path to property
* @return {any} the property value
* @public
*/
getSourceBindingProperty: function (oEvent, sPath) {
return oEvent.getSource().getBindingContext().getProperty(sPath);
},
/**
* Getter for the resource bundle
* @memberof com.sap.fiori.controller.Base
* @return {sap.ui.model.resource.ResourceModel} the resourceModel of the component
* @public
*/
getResourceBundle: function () {
return this.getOwnerComponent().getModel("i18n").getResourceBundle();
},
/**
* Getter for the resource bundle text
* @memberof com.sap.fiori.controller.Base
* @public
* @param {string} sKey - the key to retrieve the text for
* @param {string[]=} aPlaceholders - the list of parameter values which should replace the placeholders "{n}" (n is the index) in the found locale-specific string value
* @return {string} the value belonging to the key, if found. Otherwise the key itself
* @public
*/
getResourceModelText: function (sKey, aPlaceholders) {
return this.getResourceBundle().getText(sKey, aPlaceholders);
},
/**
* Convenience method to show message box
* @memberof com.sap.fiori.controller.Base
* @param {string} sI18Key - the i18n key to retrieve the text
* @param {string} [sDialogType="Default"] - the dialog type
* @param {string[]=} aI18Placeholders - the list of the parameter values which should replace the placeholders "{n}" (n is the index) in the found locale-specific string value
* @return {sap.ui.core.mvc.View} current base class for View
* @public
*/
showMessageBox: function (sI18Key, sDialogType, aI18Placeholders) {
var sMessage = this.getResourceModelText(sI18Key, aI18Placeholders);
sDialogType = sDialogType || "Default";
if (sDialogType === "Default" || sDialogType === "Warning") {
MessageBox.show(sMessage);
} else if (sDialogType === "Success") {
MessageBox.success(sMessage);
} else if (sDialogType === "Information") {
MessageBox.information(sMessage);
} else if (sDialogType === "Error") {
MessageBox.error(sMessage);
}
return this;
},
/**
* Convenience method to log any information in console
* @memberof com.sap.fiori.controller.Base
* @param {string} sI18Key - the i18n key to retrieve the text
* @return {sap.ui.mvc.View} the view instance
* @public
*/
logInfo: function (sI18Key) {
Log.info(this.getResourceModelText(sI18Key));
return this;
},
/**
* Event handler for navigating back
* It there is a history entry or an previous app-to-app navigation we go one step back in the browser history
* If not, it will replace the current entry of the browser history with the RouteOverview route.
* @memberof com.sap.fiori.controller.Base
* @public
*/
onNavBack: function () {
var oHistory = History.getInstance();
var sPreviousHash = oHistory.getPreviousHash();
if (sPreviousHash) {
window.history.go(-1);
} else {
this.navToView("RouteOverview");
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment