Version 4 of 5 · · Current version

tags
#help #example
order
130
description
an init page that uses them

Help: an init page, by example

A small, complete init page for a portfolio-tracking project, to copy from. A project's own init page is Settings:Init (open it from Admin → Init); this example lives in d2's help, so it isn't compiled or run here.

It does three things: records when the project last started, keeps a running count of holdings as they're added and removed, and notes each release.

---
tags: spec
---
# Init

What this project keeps for itself.

$class Stats (@key id: String, started: String?, holdings: Int?, lastRelease: String?)

When the project starts (and whenever this page is saved):

$when diesel.project.on.init {
  s = dom.find(cls = "Stats", key = "main") ?? {id: "main", holdings: 0}
  dom.upsert(cls = "Stats", entity = s + {started: isoDate(now())})
}

A holding added or removed: keep the count.

$when diesel.entity.on.created (category, key) if (category is "Holding") {
  s = dom.find(cls = "Stats", key = "main") ?? {id: "main", holdings: 0}
  dom.upsert(cls = "Stats", entity = s + {holdings: (s.holdings ?? 0) + 1})
}

$when diesel.entity.on.deleted (category, key) if (category is "Holding") {
  s = dom.find(cls = "Stats", key = "main") ?? {id: "main", holdings: 0}
  dom.upsert(cls = "Stats", entity = s + {holdings: (s.holdings ?? 1) - 1})
}

After a release (Admin → Project):

$when diesel.project.on.released (version) {
  s = dom.find(cls = "Stats", key = "main") ?? {id: "main", holdings: 0}
  dom.upsert(cls = "Stats", entity = s + {lastRelease: version})
}

How it works

More: LifecycleMessages for every lifecycle message and what it carries, and Expressions for the expressions.