aster.UI / Docs
Aster products
Showcase ↗

Component explorer / Application controls

Named displays: commits and people

Use trusted named renderers for scalar fields and compact related records.

Live preview

Open full size ↗

Presentation leaves stored values, sorting, filtering and exports unchanged. The unassigned author displays empty text.

Code for this example

These excerpts come from the code running above, formatted for reading. They focus on the component behaviour; imports, fixtures and page setup are available in Full application source below.

Register displays and declare their dependencies

The commit renderer uses its full raw value for copying. The person renderer receives the permitted related record. The same binding serves Grid and read-only Form.

From apps/showcase/src/examples/formatting/named-renderers.js

import { actionIcons, defineCompositeRenderer } from "@getaster/elements";
import { html } from "lit-html";
/** Reuse this registration on string fields; the stored hash stays complete. */
export const abbreviatedCommit = defineCompositeRenderer(["fieldPresentation"], ({ value }) => {
    if (typeof value !== "string" || !value)
        return "";
    return html `<span class="commit-display"><code title=${value}>${value.slice(0, 8)}</code><aster-button
    .label=${"Copy full commit"} .labelled=${false} .icon=${actionIcons.copy} .size=${"sm"} .confirmLabel=${"Copied"}
    @click=${async (event) => {
        event.stopPropagation();
        const button = event.currentTarget;
        try {
            await navigator.clipboard.writeText(value);
            button.confirmLabel = "Copied";
            button.confirm();
        }
        catch {
            button.confirmLabel = "Copy failed";
            button.confirm();
        }
    }}></aster-button></span>`;
}, {
    dependencies: {}
});
export const compactPerson = defineCompositeRenderer(["fieldPresentation"], ({ entity }) => html `<span class="compact-person"><strong>${entity.name ?? ""}</strong><a href=${`mailto:${encodeURIComponent(String(entity.email ?? "")).replace(/%40/gi, "@")}`}>${entity.email ?? ""}</a></span>`, {
    dependencies: {
        People: ["name", "email"]
    }
});
Full application source Application code & shared presentation

Download formatting.js

import { DataContext, EditModel, keyText } from "@getaster/data";
import { CompositeRegistry, bindFieldPresentations } from "@getaster/elements";
import { html, render } from "lit-html";
import { createMockService } from "@getaster/mock";
import { abbreviatedCommit, compactPerson } from "./named-renderers.js";

const out = document.querySelector("#out");
try {
  const metadata = { $Version: "4.01", $EntityContainer: "Example.Container", Example: {
    Commit: { $Kind: "EntityType", $Key: ["id"], id: { $Type: "Edm.Int32" }, hash: {}, author: { $Type: "Edm.Int32", $Nullable: true, "@Core.Description": "Author" }, author_ref: { $Kind: "NavigationProperty", $Type: "Example.Person", $ReferentialConstraint: { author: "id" } }, subject: {} },
    Person: { $Kind: "EntityType", $Key: ["id"], id: { $Type: "Edm.Int32" }, name: {}, email: {}, company: {}, phone: {}, identity_ref: { $Kind: "NavigationProperty", $Type: "Example.Person", $ReferentialConstraint: { id: "id" }, "@Core.Description": "Identity" }, reports: { $Kind: "NavigationProperty", $Type: "Example.Person", $Collection: true, "@Core.Description": "Reports" } },
    Container: { $Kind: "EntityContainer", Commits: { $Collection: true, $Type: "Example.Commit", $NavigationPropertyBinding: { author_ref: "People" } }, People: { $Collection: true, $Type: "Example.Person", $NavigationPropertyBinding: { identity_ref: "People", reports: "People" } } },
  } };
  const mock = await createMockService({ metadata, data: {
    Commits: [{ id: 1, hash: "e341e8fd05375e9225bd3c1e6f482847c113aef0", author: 1, subject: "Keep record display consistent" }, { id: 2, hash: "be415d7fe2295fc404df55ab82c20b73fbd2bb09", author: null, subject: "Handle an unassigned author" }],
    People: [{ id: 1, name: "Ada Example", email: "ada@example.com", company: "Example Company", phone: "020 7946 0100" }],
  }, wasm: new URL("../lib/odata_mock.wasm", import.meta.url) });
  const ctx = await DataContext.connect(mock.transport);
  const registry = new CompositeRegistry();
  registry.renderer("commit-short", abbreviatedCommit);
  registry.renderer("person-compact", compactPerson);
  const model = { types: {
    Commits: { fields: { hash: { type: "CommitHash" }, author: { ref: "People" } } },
    People: { fields: { name: { type: "text" }, email: { type: "text" } }, displayField: "name", display: { renderer: "person-compact" } },
  }, customTypes: { CommitHash: { base: "text", display: { renderer: "commit-short" } } } };
  const binding = bindFieldPresentations(ctx, model, "Commits", registry);
  const records = ctx.collection({ set: "Commits" });
  await records.ensure(0, 2);
  const edit = new EditModel(ctx, "Commits", keyText([1]));
  out.replaceChildren();
  render(html`<aster-grid .ctx=${ctx} .collection=${records} .label=${"Commits"} .fieldPresentation=${binding.presentation}
    .presentation=${{ columns: { hash: {title:"Commit"}, author: {title:"Author",align:"start"}, subject: {title:"Subject"} } }}
    .view=${{ columns: [{ field: "hash", title: "Commit", width: 230 }, { field: "author", title: "Author", width: 240 }, { field: "subject", title: "Subject", width: 310 }], sort: [], filters: {}, frozen: 0 }}></aster-grid>
    <h2>Read-only form</h2><aster-form .model=${edit} .readOnly=${true} .fieldPresentation=${binding.presentation}
      .sections=${[{ title: "Commit details", items: [{ field: "hash", title: "Commit" }, { field: "author", title: "Author" }, "subject"] }]}></aster-form>`, out);
  window.addEventListener("pagehide", () => { binding.dispose(); edit.dispose(); ctx.release(records); }, { once: true });
} catch (error) {
  out.setAttribute("role", "alert");
  out.textContent = `Could not load the example: ${error instanceof Error ? error.message : String(error)}`;
}

Try the workflow

  1. Compare the same commit and author in the grid and read-only form.
  2. Copy the complete hash from its abbreviated display.
  3. Inspect the dependency declarations in the source.

What the example owns

The application registers JavaScript renderers and owns the display binding. The binding requests declared related fields through normal data access and passes only permitted scalar records.