AppML 消息
AppML 消息和操作
当 AppML 即将执行一个操作时,它会将应用程序对象 ($appml) 发送到控制器。
应用程序对象的一个属性是消息 ($appml.message),它描述了应用程序的状态。
测试此消息,可以根据操作添加您自己的 JavaScript 代码。
示例
function myController($appml) {
if ($appml.message == "ready") {alert ("Hello Application");}
}
自己试试 »
AppML 消息
以下是 AppML 可以接收的消息列表
消息 | 描述 |
---|---|
"ready" | 在 AppML 初始化后发送,准备加载数据。 |
"loaded" | 在 AppML 完全加载后发送,准备显示数据。 |
"display" | 在 AppML 显示数据项之前发送。 |
"done" | 在 AppML 完成(显示完毕)后发送。 |
"submit" | 在 AppML 提交数据之前发送。 |
"error" | 在 AppML 遇到错误后发送。 |
“ready”消息
当 AppML 应用程序准备加载数据时,它会发送一个“ready”消息。
这是为应用程序提供初始数据(起始值)的理想位置
示例
<div appml-controller="myController" appml-data="customers.js">
<h1>客户</h1>
<p>{{today}}</p>
<table>
<tr>
<th>客户</th>
<th>城市</th>
<th>国家</th>
</tr>
<tr appml-repeat="records">
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
<p>Copyright {{copyright}}</p>
</div>
<script>
function myController($appml) {
if ($appml.message == "ready") {
$appml.today = new Date();
$appml.copyright = "W3Schools"
}
}
</script>
自己试试 »
在上面的示例中,当 $appml.message 为“ready”时,控制器会向应用程序添加两个新属性(today 和 copyright)。
当应用程序运行时,这些新属性对应用程序可用。
“loaded”消息
当 AppML 应用程序加载了数据(准备显示)后,它会发送一个“loaded”消息。
这是提供加载数据更改(如有必要)的理想位置。
示例
function myController($appml) {
if ($appml.message == "loaded") {
// compute your values here before display
}
}
“display”消息
每次 AppML 显示数据项时,它都会发送一个“display”消息。
这是修改输出的理想位置
示例
<div appml_app="myController" appml-data="customers.js">
<h1>客户</h1>
<table>
<tr>
<th>客户</th>
<th>城市</th>
<th>国家</th>
</tr>
<tr appml-repeat="records">
<td>{{CustomerName}}</td>
<td>{{City}}</td>
<td>{{Country}}</td>
</tr>
</table>
</div>
<script>
function myController($appml) {
if ($appml.message == "display") {
if ($appml.display.name == "CustomerName") {
$appml.display.value = $appml.display.value.substr(0,15);
}
if ($appml.display.name == "Country") {
$appml.display.value = $appml.display.value.toUpperCase();
}
}
}
</script>
自己试试 »
在上面的示例中,“CustomerName”被截断为 15 个字符,而“Country”被转换为大写。
“done”消息
当 AppML 应用程序完成数据显示后,它会发送一个“done”消息。
这是清理或计算应用程序数据(显示后)的理想位置。
示例
<script>
function myController($appml) {
if ($appml.message == "done") {
calculate data here
}
}
</script>
“submit”消息
当 AppML 应用程序准备提交数据时,它会发送一个“submit”消息。
这是验证应用程序输入的理想位置。
示例
<script>
function myController($appml) {
if ($appml.message == "submit") {
validate data here
}
}
</script>
“error”消息
如果发生错误,AppML 会发送一个“error”消息。
这是处理错误的理想位置。
示例
<script>
function myController($appml) {
if ($appml.message == "error") {
alert ($appml.error.number + " " + $appml.error.description)
}
}
</script>
AppML 属性
这是常用 AppML 属性列表
属性 | 描述 |
---|---|
$appml.message | 应用程序的当前状态。 |
$appml.display.name | 即将显示的数据字段的名称。 |
$appml.display.value | 即将显示的数据字段的值。 |
$appml.error.number | 错误编号。 |
$appml.error.description | 错误描述。 |