AppML 架构

模型、数据源、模板、控制器四层,以及请求如何流转。

Intermediate
Key PointsAppML 把「数据从哪来」「字段是什么」「怎么显示」拆成三层,控制器负责粘合与自定义逻辑。

四层结构

Text
index.html
   │  引用 appml.js,声明 <appml-app src="model.xml">
   ▼
model.xml ──── datasource ──── 数据源(数据库 / JSON / REST)
   │        └── items ──────── 字段定义(类型、标签、校验)
   │        └── html ───────── 页面标题、布局提示
   ▼
template(可选)── 自定义列表行与表单的 HTML 结构
   ▼
controller(可选)── 自定义 JS:事件、格式化、联动

模型的三块

XML
<appml>
  <!-- 1. 数据源 -->
  <datasource>
    <database name="demo"></database>
    <query>SELECT id, name, city, credit FROM customers</query>
  </datasource>

  <!-- 2. 字段定义 -->
  <items>
    <item name="id"     label="编号" type="number" primarykey="true"/>
    <item name="name"   label="姓名" required="true"/>
    <item name="city"   label="城市"/>
    <item name="credit" label="额度" type="number"/>
  </items>

  <!-- 3. 页面信息 -->
  <html>
    <title>客户管理</title>
  </html>
</appml>

items is the heart of the model: the type picks the form control (number → numeric input, date → date picker), required drives validation, and primarykey decides which field identifies a record for updates and deletes.

数据源的类型

TypeDeclarationUse
Database<database name="x"> + <query>需要服务端中间层
JSON file<query> 指向 .json静态演示
REST API模型里写 URL真实项目
XML
<!-- 直接读一个 JSON 文件,不需要后端 -->
<appml>
  <datasource>
    <query>data/customers.json</query>
  </datasource>
  <items>
    <item name="id"   label="编号"/>
    <item name="name" label="姓名"/>
  </items>
</appml>

模板的作用

HTML
<appml-app src="model.xml">
  <appml-template view="list">
    <div class="card">
      <h3>{{name}}</h3>
      <p>{{city}} · 额度 {{credit}}</p>
    </div>
  </appml-template>
</appml-app>

Without a template AppML renders a default table; with one it follows your HTML and {{field}} placeholders get replaced. That lets you apply Bootstrap or your own CSS classes.

控制器的位置

JavaScript
function myController(appml) {
  appml.onload = function () {
    console.log('数据加载完成,共 ' + appml.data.length + ' 条');
  };
  appml.onerror = function (err) {
    console.error('加载失败', err);
  };
}
HTML
<appml-app src="model.xml" controller="myController"></appml-app>
AppML controllers are global function names given as strings, not modules. With several apps on one page the names must be unique or the later one overwrites the earlier — a nasty bug in single-page setups.

请求流转

  1. 页面解析 <appml-app>,按 src 读取模型
  2. 按 datasource 取数据(文件或接口)
  3. 按 items 与模板渲染列表 / 表单
  4. 用户操作触发增删改,按 primarykey 定位记录
  5. 回调控制器里的 onload / onerror

Friends & Partners