Tin nhắn AppML


Thông báo và hành động trong 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) đến bộ điều khiển.

Một trong những thuộc tính của đối tượng ứng dụng là thông báo ($ appml.message), mô tả trạng thái ứng dụng.

Kiểm tra thông báo này, cho phép bạn thêm mã JavaScript của riêng mình, tùy thuộc vào hành động.

Thí dụ

function myController($appml) {
    if ($appml.message == "ready") {alert ("Hello Application");}
}

Tin nhắn AppML

Đây là danh sách các thông báo 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 báo "sẵn sàng"

Khi một ứng dụng AppML sẵn sàng tải dữ liệu, nó sẽ gửi một 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):

Thí 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>

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 ( ngày naybản quyền ).

Khi ứng dụng chạy, các thuộc tính mới có sẵn cho ứng dụng.


Tin nhắn "đã tải"

Khi một ứng dụng AppML được tải với dữ liệu (sẵn sàng hiển thị), nó sẽ gửi một 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) đối với dữ liệu đã tải.

Thí dụ

function myController($appml) {
    if ($appml.message == "loaded") {
        // compute your values here before display
    }
}

Thông báo "hiển thị"

Mỗi khi AppML đang hiển thị một mục dữ liệu, nó sẽ gửi một thông báo " hiển thị ".

Đây là nơi hoàn hảo để sửa đổi đầu ra:

Thí 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>

Trong ví dụ trên, "Tên khách hàng" được cắt ngắn còn 15 ký tự và "Quốc gia" được chuyển thành chữ hoa.


Thông báo "xong"

Khi một ứng dụng AppML hoàn tất việc hiển thị dữ liệu, nó sẽ gửi một thông báo " xong ".

Đâ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ị).

Thí dụ

<script>
function myController($appml) {
    if ($appml.message == "done") {
        calculate data here
    }
}
</script>

Thông báo "gửi"

Khi một ứng dụng AppML đã sẵn sàng để gửi dữ liệu, nó sẽ gửi một thông báo " gửi ".

Đây là nơi hoàn hảo để xác nhận đầu vào của ứng dụng.

Thí 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ý các lỗi.

Thí 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.