AppML- boodskappe
AppML-boodskappe en aksies
Wanneer AppML op die punt is om 'n aksie uit te voer, stuur dit die toepassingsobjek ($appml) na die kontroleerder.
Een van die toepassingsvoorwerp se eienskappe is 'n boodskap ($appml.message), wat die toepassingstatus beskryf.
Deur hierdie boodskap te toets, kan jy jou eie JavaScript-kode byvoeg, afhangende van die aksie.
Voorbeeld
function myController($appml) {
if ($appml.message == "ready") {alert ("Hello
Application");}
}
AppML-boodskappe
Dit is 'n lys van AppML-boodskappe wat ontvang kan word:
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. |
Die "gereed" Boodskap
Wanneer 'n AppML-toepassing gereed is om data te laai, sal dit 'n "gereed"-boodskap stuur.
Dit is die perfekte plek om die toepassing van aanvanklike data (beginwaardes) te voorsien:
Voorbeeld
<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>
In die voorbeeld hierbo, wanneer die $appml.message "gereed" is, voeg die beheerder twee nuwe eienskappe by die toepassing ( vandag en kopiereg ).
Wanneer die toepassing loop, is die nuwe eienskappe vir die toepassing beskikbaar.
Die "gelaaide" Boodskap
Wanneer 'n AppML-toepassing met data gelaai is (gereed om te vertoon), sal dit 'n " gelaaide " boodskap stuur.
Dit is die perfekte plek om veranderinge (indien nodig) aan die gelaaide data te verskaf.
Voorbeeld
function myController($appml) {
if ($appml.message == "loaded") {
// compute your values here before display
}
}
Die "vertoon" Boodskap
Elke keer as AppML 'n data-item vertoon, sal dit 'n " vertoon "-boodskap stuur.
Dit is die perfekte plek om die uitset te verander:
Voorbeeld
<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>
In die voorbeeld hierbo word "CustomerName" afgekap tot 15 karakters, en "Country" word omgeskakel na hoofletters.
Die "klaar" Boodskap
Wanneer 'n AppML-toepassing klaar data vertoon het, sal dit 'n " klaar "-boodskap stuur.
Dit is die perfekte plek om toepassingsdata skoon te maak of te bereken (na vertoon).
Voorbeeld
<script>
function myController($appml) {
if ($appml.message == "done") {
calculate data here
}
}
</script>
Die "submit" Boodskap
Wanneer 'n AppML-toepassing gereed is om data in te dien, sal dit 'n " indien "-boodskap stuur.
Dit is die perfekte plek om aansoekinvoer te valideer.
Voorbeeld
<script>
function myController($appml) {
if ($appml.message == "submit") {
validate data here
}
}
</script>
Die "fout" Boodskap
As 'n fout voorkom, sal AppML 'n " fout "-boodskap stuur.
Dit is die perfekte plek om foute te hanteer.
Voorbeeld
<script>
function myController($appml) {
if ($appml.message ==
"error") {
alert ($appml.error.number + " " + $appml.error.description)
}
}
</script>
AppML-eienskappe
Dit is 'n lys van 'n paar algemeen gebruikte AppML-eienskappe:
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. |