此前的五篇全是代码。C 固件与 Dart 服务器。每次都有编译在跑。
这一篇没有编译。我们要做无人店铺店主看到的应用,而源码是四个 JSON 文件。
unmanned_store.mbd/
manifest.json 它是谁
ui/app.json 路由
ui/pages/main.json 画面一
ui/pages/restock.json 画面二
而且在这篇的后半,我们会真的跑一遍「改了那些 JSON 应用就变」。构建不会运行。
先看结果
店主看到的第一个画面。今天的营业额和库存,以及有几样需要补货。

按下「What needs a visit」,就跳到同一个文件夹里的另一个画面。

bring N按下「Order the lot」,列表清空。

现在我们改 JSON 再跑一次。 编译器没有执行。

请仔细看最后这张。标签变成了洗衣店,而商品依然是冰淇淋。这个只变了一半的画面,是这一篇里最重要的一张图。后面还会回来。
包是什么
manifest.json 说明这个应用是谁。不是代码,是身份。
{
"schemaVersion": "1.0.0",
"manifest": {
"id": "com.makemind.sample.unmanned_store",
"name": "Unmanned Store",
"type": "application",
"entryPoint": "ui.app",
"description": "What the owner of an unmanned store sees: stock, takings and what needs a visit. No code, only declarations.",
"category": "business",
"tags": ["retail", "unmanned", "sample"]
}
}
ui/app.json 是画面的地图。
{
"type": "application",
"title": "Unmanned Store",
"initialRoute": "/",
"routes": {
"/": "ui://pages/main",
"/restock": "ui://pages/restock"
}
}
而每一个页面就是一个画面。按钮做什么也写在这里 — 不是函数名,而是工具名。
{
"type": "button",
"label": "What needs a visit",
"variant": "elevated",
"onTap": { "type": "navigation", "action": "push", "route": "/restock" }
},
{
"type": "button",
"label": "Refresh",
"variant": "outlined",
"onTap": { "type": "tool", "tool": "store.today", "params": {} }
}
服务器托管这个文件夹 — 四十行
这个文件夹在 AppPlayer 里的打开方式,和这个系列里其他服务器应用一样:由店铺自己的服务器托管。serve_bundle.dart 就是全部 — 注册 ui://app,再为每个文件注册一个 ui://pages/<名字>,每次请求都重新读文件。之所以短,是因为这个格式不要求更多:播放器本来就会画画面定义,而包不过是给画面定义加了一份目录。
void registerBundleUi(Server server, String bundleDir) {
final manifest = _json('$bundleDir/manifest.json')['manifest'];
void serve(String uri, String name, String description,
Map<String, dynamic> Function() document) {
server.addResource(
uri: uri, name: name, description: description,
mimeType: 'application/json',
handler: (requestedUri, params) async => ReadResourceResult(contents: [
ResourceContentInfo(uri: requestedUri,
mimeType: 'application/json', text: jsonEncode(document())),
]),
);
}
serve('ui://app', manifest['name'], 'The app: routes and theme',
() => _json('$bundleDir/ui/app.json'));
for (final f in Directory('$bundleDir/ui/pages').listSync().whereType<File>()) {
if (!f.path.endsWith('.json')) continue;
final name = f.uri.pathSegments.last.replaceAll('.json', '');
serve('ui://pages/$name', name, 'Screen "$name"', () => _json(f.path));
}
}
验证在包发出去之前,先确认文件夹里没有任何需要构建的东西。
# If there is a build step this article's argument collapses, so start by
# checking there is nothing to build
BUILDISH=$(find unmanned_store.mbd -type f ! -name '*.json' | wc -l | tr -d ' ')
[ "$BUILDISH" -eq 0 ] || { echo "bundle contains non-json files"; exit 1; }
页面进入时自己把状态填上
第一次跑的时候第二个画面是空的。 服务器说有三行短缺,截图却是「Nothing to bring」。
原因在格式里面。每个页面都带着自己的 initialState。
"initialState": { "low": [], "lowCount": 0, "notice": "" }
一换路由,新画面就从自己的初始值开始。上一个画面收到的数据不会跟过来。所以每个页面写明进入时要问什么,运行时把答案并进页面状态。
"onInit": { "type": "tool", "tool": "store.today", "params": {} }
也放进了验证。补货画面要等列表上来了才拍,下单必须把列表清空,而不是只换个说法。
ap.tap("What needs a visit")
ap.wait_text("bring") # photographed only once the list is on it
ap.shot("02_restock.png")
ap.tap("Order the lot")
ap.wait_text("Ordered 3 lines")
ap.expect_no_text("bring 13") # the ordered line is gone, not just relabelled