AppMLメッセージ
AppMLメッセージとアクション
AppMLがアクションを実行しようとすると、アプリケーションオブジェクト($ appml)がコントローラーに送信されます。
アプリケーションオブジェクトのプロパティの1つは、アプリケーションの状態を説明するメッセージ($ appml.message)です。
このメッセージをテストすると、アクションに応じて独自のJavaScriptコードを追加できます。
例
function myController($appml) {
if ($appml.message == "ready") {alert ("Hello
Application");}
}
AppMLメッセージ
これは、受信できるAppMLメッセージのリストです。
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. |
「準備完了」メッセージ
AppMLアプリケーションがデータをロードする準備ができると、「準備完了」メッセージを送信します。
これは、アプリケーションに初期データ(開始値)を提供するのに最適な場所です。
例
<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>
上記の例では、$ appml.messageが「準備完了」になると、コントローラーは2つの新しいプロパティ(todayとcopyright)をアプリケーションに追加します。
アプリケーションが実行されると、アプリケーションで新しいプロパティを使用できるようになります。
「ロードされた」メッセージ
AppMLアプリケーションにデータがロードされると(表示の準備ができている)、「ロードされた」メッセージが送信されます。
これは、ロードされたデータに(必要に応じて)変更を加えるのに最適な場所です。
例
function myController($appml) {
if ($appml.message == "loaded") {
// compute your values here before display
}
}
「表示」メッセージ
AppMLがデータ項目を表示するたびに、「表示」メッセージを送信します。
これは、出力を変更するのに最適な場所です。
例
<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>
上記の例では、「CustomerName」は15文字に切り捨てられ、「Country」は大文字に変換されます。
「完了」メッセージ
AppMLアプリケーションがデータの表示を終了すると、「完了」メッセージを送信します。
これは、アプリケーションデータをクリーンアップまたは計算するのに最適な場所です(表示後)。
例
<script>
function myController($appml) {
if ($appml.message == "done") {
calculate data here
}
}
</script>
「送信」メッセージ
AppMLアプリケーションがデータを送信する準備ができると、「送信」メッセージを送信します。
これは、アプリケーション入力を検証するのに最適な場所です。
例
<script>
function myController($appml) {
if ($appml.message == "submit") {
validate data here
}
}
</script>
「エラー」メッセージ
エラーが発生した場合、AppMLは「エラー」メッセージを送信します。
これは、エラーを処理するのに最適な場所です。
例
<script>
function myController($appml) {
if ($appml.message ==
"error") {
alert ($appml.error.number + " " + $appml.error.description)
}
}
</script>
AppMLプロパティ
これは、一般的に使用されるAppMLプロパティのリストです。
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. |