New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

ui5-typed-model

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ui5-typed-model

A strictly typed wrapper of a UI5 JSON model

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

UI5 TypedModel

API Reference

A strictly typed wrapper of a UI5 JSON model.

Installation

Use npm to install:

npm install ui5-typed-model

Please note that this library works with TypeScript v5.0 or higher and depends on the official ESM-style UI5 types @openui5/types or @sapui5/types which will not be automatically installed.

Furthermore, a code transformation is needed to transform UI5-style ESM imports into valid UI5 code, e.g. with Babel and either babel-plugin-ui5-esm or babel-plugin-transform-modules-ui5.

Usage

Creating a Model

Create a new TypedModel with an underlying JSONModel by calling the constructor function and optionally provide a type:

import { TypedModel } from "ui5-typed-model";

interface Model {
  message: string;
  name: string;
  contacts: {
    name: string;
    email: string;
  }[];
}

const model = new TypedModel<Model>({
  message: "Greetings",
  name: "Universe",
  contacts: [
    { name: "Yichuan", email: "me@site.com" },
    { name: "Ryan", email: "you@site.com" },
  ],
});

// Binding the model to core for simplicity
model.bindTo(sap.ui.getCore());

You can safely get and set property values using typed path builders:

const name = model.get((data) => data.name);
// name: string

const contactEmail = model.get(
  (data) => data.contacts[0].email
);
// contactEmail: string | undefined

model.set((data) => data.message, "Hello");

Data Binding

There are convenience methods to create PropertyBindingInfo and AggregationBindingInfo objects:

import Text from "sap/m/Text";
import VBox from "sap/m/VBox";

// ...

const text = new Text();

text.bindProperty(
  "text",
  model.binding((data) => data.message)
);

const vbox = new VBox();

vbox.bindAggregation(
  "items",
  model.aggregationBinding(
    (data) => data.contacts,
    (id, model) => {
      const text = new Text(id);

      text.setBindingContext(model.context);
      text.bindProperty(
        "text",
        model.binding((_, context) => context.email)
      );

      return text;
    }
  )
);

Data Transformation

You can use map to transform bindings:

const text = new Text();

text.bindProperty(
  "text",
  model
    .binding((data) => data.message)
    .map((message) => `${message}! And Goodbye!`)
);

It is possible to create composite bindings with the helper function expressionBinding:

import { compositeBinding } from "ui5-typed-model";

// ...

const text = new Text();

text.bindProperty(
  "text",
  compositeBinding(
    [model.binding((data) => data.message), model.binding((data) => data.name)],
    (message, name) => `${message}, ${name}!`
  )
);

Keywords

ui5

FAQs

Package last updated on 17 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts