Tin nhắn AppML
Thông báo và hành động của AppML
Khi AppML chuẩn bị thực hiện một hành động, nó sẽ gửi đối tượng ứng dụng ($appml) tới bộ điều khiển.
Một trong những thuộc tính của đối tượng ứng dụng là một thông báo ($appml.message), mô tả trạng thái ứng dụng.
Việc kiểm tra thông báo này sẽ cho phép bạn thêm mã JavaScript của riêng mình, tùy thuộc vào hành động.
Ví dụ
function myController($appml) {
if ($appml.message == "ready") {alert ("Hello
Application");}
}
Hãy tự mình thử »Tin nhắn AppML
Đây là danh sách các tin nhắn AppML có thể nhận được:
Message | Description |
---|---|
"ready" | Sent after AppML is initiated, and ready to load data. |
"loaded" | Sent after AppML is fully loaded, ready to display data. |
"display" | Sent before AppML displays a data item. |
"done" | Sent after AppML is done (finished displaying). |
"submit" | Sent before AppML submits data. |
"error" | Sent after AppML has encountered an error. |
Thông điệp “sẵn sàng”
Khi ứng dụng AppML sẵn sàng tải dữ liệu, nó sẽ gửi thông báo "sẵn sàng".
Đây là nơi hoàn hảo để cung cấp cho ứng dụng dữ liệu ban đầu (giá trị bắt đầu):
Ví dụ
<div appml-controller="myController" appml-data="customers.js">
<h1>Customers</h1>
<p>{{today}}</p>
<table>
<tr>
<th>Customer</th>
<th>City</th>
<th>Country</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>
Hãy tự mình thử »Trong ví dụ trên, khi $appml.message "sẵn sàng", bộ điều khiển sẽ thêm hai thuộc tính mới vào ứng dụng ( hôm nay và bản quyền ).
Khi ứng dụng chạy, các thuộc tính mới sẽ có sẵn cho ứng dụng.
Tin nhắn "đã tải"
Khi một ứng dụng AppML được tải dữ liệu (sẵn sàng hiển thị), nó sẽ gửi thông báo " đã tải ".
Đây là nơi hoàn hảo để cung cấp các thay đổi (nếu cần) cho dữ liệu được tải.
Ví dụ
function myController($appml) {
if ($appml.message == "loaded") {
// compute your values here before display
}
}
Thông báo "hiển thị"
Mỗi lần AppML hiển thị một mục dữ liệu, nó sẽ gửi thông báo " display ".
Đây là nơi hoàn hảo để sửa đổi đầu ra:
Ví dụ
<div appml_app="myController" appml-data="customers.js">
<h1>Customers</h1>
<table>
<tr>
<th>Customer</th>
<th>City</th>
<th>Country</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>
Hãy tự mình thử »Trong ví dụ trên, "Tên khách hàng" bị cắt ngắn còn 15 ký tự và "Quốc gia" được chuyển thành chữ hoa.
Thông báo "hoàn thành"
Khi một ứng dụng AppML hiển thị xong dữ liệu, nó sẽ gửi thông báo " done ".
Đây là nơi hoàn hảo để dọn dẹp hoặc tính toán dữ liệu ứng dụng (sau khi hiển thị).
Ví dụ
<script>
function myController($appml) {
if ($appml.message == "done") {
calculate data here
}
}
</script>
Tin nhắn "gửi"
Khi một ứng dụng AppML sẵn sàng gửi dữ liệu, nó sẽ gửi thông báo " submit ".
Đây là nơi hoàn hảo để xác nhận đầu vào của ứng dụng.
Ví dụ
<script>
function myController($appml) {
if ($appml.message == "submit") {
validate data here
}
}
</script>
Thông báo "lỗi"
Nếu xảy ra lỗi, AppML sẽ gửi thông báo " lỗi ".
Đây là nơi hoàn hảo để xử lý lỗi.
Ví dụ
<script>
function myController($appml) {
if ($appml.message ==
"error") {
alert ($appml.error.number + " " + $appml.error.description)
}
}
</script>
Thuộc tính AppML
Đây là danh sách một số thuộc tính AppML thường được sử dụng:
Property | Description |
---|---|
$appml.message | The current state of the application. |
$appml.display.name | The name of the data field about to be displayed. |
$appml.display.value | The value of the data field about to be displayed. |
$appml.error.number | The error number. |
$appml.error.description | The error description. |