AppMLコントローラー


AppMLコントローラー の目的は、アプリケーション制御できるようにすることです。


コントローラーは何ができますか?

  • 初期データを設定する
  • アプリケーションデータを変更する
  • 入力と出力を処理する
  • データを検証する
  • データを要約する
  • エラーを処理する
  • アプリケーションの開始と停止
  • そしてもっとたくさん

コントローラーなし

デフォルトでは、AppMLアプリケーションはコントローラーなしで実行されます。

<table appml-data="customers.js">
<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>

コントローラー付き

AppMLコントローラーを使用すると、 JavaScriptを使用してアプリケーションを 制御できます。

コントローラは、あなたが提供するJavaScript関数です

appml -controller属性は、コントローラー関数を参照するために使用されます。

<h1>Customers</h1>
<table appml-data="customers.js" appml-controller="myController">
  <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>

<script>
function myController($appml) {
    if ($appml.message == "display") {
        if ($appml.display.name == "CustomerName") {
            $appml.display.value = $appml.display.value.toUpperCase();
        }
    }
}
</script>

上記の例のコントローラー(myController)は、表示される前に「CustomerName」の値を大文字に変更します。

コントローラーがある場合、AppMLは、重要なアクションごとに、アプリケーションオブジェクト ($ appml)をコントローラーに送信します。

アプリケーションのプロパティの1つは、アプリケーションの状態を説明するメッセージ($ appml.message)です。

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.

メッセージについては、次の章で説明します。