Skip to content

Instantly share code, notes, and snippets.

@htammen
Last active July 17, 2019 13:27
Show Gist options
  • Save htammen/c9e5167570fa88cd436d5aa72dd1c512 to your computer and use it in GitHub Desktop.
Save htammen/c9e5167570fa88cd436d5aa72dd1c512 to your computer and use it in GitHub Desktop.
BaseController.ts (Typescript version of UI5 BaseController from which view controller inherit)
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/core/routing/History",
"de/tammenit/ui5/AppAdminApp/Component",
"sap/ui/model/json/JSONModel"
], function(Controller: typeof sap.ui.core.mvc.Controller,
History: typeof sap.ui.core.routing.History,
Component: typeof de.tammenit.ui5.AppAdminApp.Component,
JSONModel: typeof sap.ui.model.json.JSONModel) {
"use strict";
class BaseController extends Controller {
private model: sap.ui.model.json.JSONModel;
constructor() {
// it's important to call the extend method here. It creates metadata that is used in UI5 apps via
// the method getMetadata. Hence we also assign this method to the prototype of our class.
let fnClass = Controller.extend("de.tammenit.ui5.AppAdminApp.controller.BaseController", {});
BaseController.prototype.getMetadata = fnClass.prototype.getMetadata;
super("de.tammenit.ui5.AppAdminApp.controller.BaseController");
}
public onInit(): void {
}
public getOwnerAppComponent(): de.tammenit.ui5.AppAdminApp.Component {
// this double cast is weired but is documented here: https://basarat.gitbooks.io/typescript/docs/types/type-assertion.html
let comp = this.getOwnerComponent() as any as de.tammenit.ui5.AppAdminApp.Component;
return comp;
}
/**
* get the Router for this view
*
* @returns {*}
* @memberof BaseController
*/
getRouter(): any {
return Component.getRouterFor(this);
}
getModel(sName?: string): sap.ui.model.json.JSONModel {
if(sName) {
return <sap.ui.model.json.JSONModel>this.getView().getModel(sName);
} else {
return <sap.ui.model.json.JSONModel>this.getView().getModel();
}
}
getResourceBundle(): {getText(value: string): string, setText(value: string): void} {
return (<sap.ui.model.resource.ResourceModel>this.getOwnerAppComponent().getModel("i18n")).getResourceBundle()
}
onNavBack(): void {
var oHistory, sPreviousHash;
oHistory = History.getInstance();
sPreviousHash = oHistory.getPreviousHash();
if (sPreviousHash !== undefined) {
window.history.go(-1);
} else {
this.getRouter().navTo("RouteMain", {}, true /*no history*/);
}
}
}
return BaseController;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment