示例:计数器
最小的完整 QORM 应用——全局状态、一个动作和一个绑定。源码: examples/counter。
qorm run examples/counter
按 + / -:按钮派发一个动作,运行时更新状态,绑定的文本随之重新渲染。
组成部分
全局状态,在 qorm.json 中声明:
"globalState": { "schema": { "count": "number" }, "initial": { "count": 0 } }
计数值,在场景中绑定:
{ "type": "text", "id": "number", "text": "{{state.count}}" }
按钮调用一个动作,并将当前值作为参数传入:
{ "type": "button", "id": "btn_plus", "label": "+",
"onPress": { "type": "invoke", "name": "increment", "args": { "count": "{{state.count}}" } } }
该动作(actions/increment.json)计算出新值:
{ "type": "action", "id": "increment",
"steps": [ { "type": "state.set", "path": "count", "value": "{{ count + 1 }}" } ] }
格式说明(这是可运行的格式)
- 文本使用
text字段(而非value);用{{ state.x }}绑定。 - 按钮的回调是
onPress(而非on: { press }),用于指定一个动作。 - 在动作内部,
value能看到来自onPress的参数(此处为count),因此
{{ count + 1 }} 可以生效。[JSON format spec] 设计意图草案与此有出入—— 请以示例为准。