Orders
An order list. Orders are fetched from the server. The user can search, filter by status, and sort by column. Clicking a row opens the detail pane. Changing the quantity and saving shows a notification.
The designer's HTML is used as-is
index.html contains nothing specific to LPScript. The ids and classes are there for the CSS. The program clones the sample elements and fills in values.
Screen >> rowOf: o = (
r := Html clone: '#lines tr' fill: {
'.c-id' -> o id.
'.c-customer' -> o customer.
'.c-total' -> (self yen: o total).
'.c-status .badge' -> o status label }.
(r on: #click send: #select: with: o id) key: o id ).
If the designer adds a column and keeps the class names, the program does not change. If a class name changes, the build fails.
Status is a class
An order's status is one of pending, in progress, or done. Represented as strings, a misspelling or a missing comparison is not detected until run time. Instead, each status is a class, and a protocol declares the methods they must implement.
Protocol subclass: #Status requires: (#label #badge #next).
Object subclass: #Pending protocols: (Status).
Pending >> label = '未処理'.
Pending >> badge = 'b-new'.
Pending >> next = Working new as: Status.
The string '未処理' appears only here. The filter button label, the match when reading JSON, the CSS class, and the transition to the next status all derive from this definition. Adding a status and omitting badge is a compile error.
Not waiting for the response
The handler sends the request and returns. The response arrives as a message.
Screen >> load = (
(Http get: 'orders.json') then: [:r | me ref tell loaded: r].
self ).
The screen keeps responding to input while the request is in flight. Http get: does not throw. A failed connection returns a response with status 0, so error handling is in one place.
Money
qty is an Int and price is a Decimal. The total is multiplied and then truncated to whole yen.
Order >> total = (qty asDecimal * price) truncatedTo: 0.
Reading from JSON does not lose digits. Numbers are kept as the text they arrived as and become Decimal when asDecimal is called.