import Button from "sap/m/Button";
const oButton = this.byId("idButtonPost") as Button;
((this.byId("idListOrders") as List).getBinding("items") as JSONListBinding).filter(aFilters);
Spoiler alert: in the end of this topic it will transform to:
this.byId("idListOrders").getBinding<JSONListBinding>("items").filter(aFilters);
import UI5Element from "sap/ui/core/Element";
import Controller from "sap/ui/core/mvc/Controller";
/**
* @namespace ui5.typescript.helloworld.controller
*/
export default class BaseController<
ViewFragmentIds extends Record<string, UI5Element>
> extends Controller {
byId<T extends keyof ViewFragmentIds>(sId: T): ViewFragmentIds[T] {
return super.byId(sId as string) as ViewFragmentIds[T];
}
}
import Button from "sap/m/Button";
import List from "sap/m/List";
export type MasterView = {
idListOrder: List;
idButtonPost: Button;
}
<mvc:View
controllerName="ui5.typescript.helloworld.controller.Master"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
displayBlock="true"
height="100%"
busyIndicatorDelay="0"
>
<Page>
<List id="idListOrders"/>
<Button
id="idButtonPost"
type="Accept"
text="Post"
/>
</Page>
</mvc:View>
import BaseController from "./BaseController";
import { MasterView } from "./ViewFragmentIds";
/**
* @namespace ui5.typescript.helloworld.controller
*/
export default class Master extends BaseController<MasterView> {
onInit() {
}
}
import Button from "sap/m/Button";
import List from "sap/m/List";
import Table from "sap/m/Table";
export type MasterView = {
idListOrder: List;
idButtonPost: Button;
};
export type OrderItemValueHelpDialogFragment = {
idTableOrderItems: Table;
};
import BaseController from "./BaseController";
import { MasterView, OrderItemValueHelpDialogFragment } from "./ViewFragmentIds";
/**
* @namespace ui5.typescript.helloworld.controller
*/
export default class Master extends BaseController<MasterView & OrderItemValueHelpDialogFragment> {
onInit() {
const oList = this.byId("idListOrder");
const oTable = this.byId("idTableOrderItems");
}
}
UI5: (TS) Generate types for XML files (id to class mapping)
import JSONModel from "sap/ui/model/json/JSONModel";
import ODataModel from "sap/ui/model/odata/v2/ODataModel";
export type ManifestModels = {
S4HANAModel: ODataModel;
MasterModel: JSONModel;
}
import { ManifestModels } from "./ManifestModels";
declare module "sap/ui/base/ManagedObject" {
export default interface ManagedObject {
getModel<T extends keyof ManifestModels>(sModelName: T): ManifestModels[T];
}
}
Result: getModel method now understands, that "MasterModel" is JSONModel:
import Context from "sap/ui/model/Context";
declare module "sap/ui/model/json/JSONModel" {
export default interface JSONModel {
getProperty<T>(sPath: string, oContext?: Context): T;
}
}
export interface Order {
OrderID: string;
Description: string;
OrderItemQuantity: number;
}
UI5: (TS) Generate interfaces for OData entities
export interface CatalogKeys {
id: string;
}
export interface Catalog extends CatalogKeys {
type: string;
domainId: string;
scope: string;
baseUrl: string;
/** @description Counter */
chipCount: string;
Chips?: { results: Chip[] };
}
export type EntitySets = {
"Catalogs": {
keys: CatalogKeys;
type: Catalog;
typeName: "Catalog";
};
}
import ODataModel from "sap/ui/model/odata/v2/ODataModel";
import { EntitySets } from "../util/ODataEntities";
type TCreateParameters = Parameters<typeof ODataModel.prototype.create>["2"];
/**
* @namespace ui5.typescript.helloworld.control
*/
export default class CustomODataModel extends ODataModel {
createAsync<EntitySet extends keyof EntitySets & string>(
sPath: `/${EntitySet}`,
mData: EntitySets[EntitySet]["keys"],
mOptions: TCreateParameters = {}
): Promise<EntitySets[EntitySet]["type"]> {
return new Promise((resolve, reject) => {
mOptions.success = resolve;
mOptions.error = reject;
this.create(sPath, mData, mOptions);
});
}
}
private async _createCatalog() {
const oODataModel = this.getView().getModel("CustomODataModel");
const mCreatedCatalog = await oODataModel.createAsync("/Catalogs", {
id: "123",
});
}
declare module "sap/ui/base/ManagedObject" {
export default interface ManagedObject {
getBinding<T extends Binding = Binding>(sName: string): T;
}
}
//example of usage
this.byId("idTableOrders").getBinding<JSONListBinding>("items").filter(aFilters);
declare module "sap/ui/model/Context" {
export default interface Context {
getObject<T>(sPath?: string | undefined, mParameters?: object | undefined): T;
getProperty<T>(sPath: string): T;
}
}
//example of usage
const oSelectedOrderItem = this.byId("idTableOrders").getSelectedItem();
const mOrder = oSelectedOrderItem.getBindingContext().getObject<Order>();
const sOrderId = oSelectedOrderItem.getBindingContext().getProperty<string>("OrderID");
npm install ts-json-as-const
npx ts-json-as-const ./src/manifest.json
import * as Manifest from "../manifest.json";
const aRoutes = Manifest["sap.ui5"].routing.routes.map((route) => route.name);
const aTargets = Manifest["sap.ui5"].routing.routes.map((test) => test.name);
export type Routes = typeof aRoutes[number];
export type Targets = typeof aTargets[number];
export type RouterArgs = {
[key: Routes]: object;
Master: {
args: {};
};
Detail: {
args: {
documentId: string;
};
};
};
export type MasterArgs = RouterArgs["Master"]["args"];
export type DetailArgs = RouterArgs["Detail"]["args"];
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
10 | |
9 | |
7 | |
4 | |
4 | |
3 | |
3 | |
3 | |
2 |