Contrôleurs AppML


Le but d'un contrôleur AppML est de vous permettre de contrôler votre application.


Que peut faire un contrôleur ?

  • Définir les données initiales
  • Modifier les données de l'application
  • Gérer l'entrée et la sortie
  • Valider les données
  • Résumer les données
  • Gérer les erreurs
  • Démarrer et arrêter des applications
  • Et beaucoup plus

Sans contrôleur

Par défaut, les applications AppML s'exécutent sans contrôleur :

Exemple

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

Avec un contrôleur

Avec un contrôleur AppML, vous pouvez contrôler votre application avec JavaScript .

Le contrôleur est une fonction JavaScript, fournie par vous .

L' attribut appml-controller est utilisé pour faire référence à une fonction de contrôleur.

Exemple

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

Le contrôleur (myController) dans l'exemple ci-dessus, change la valeur de "CustomerName" en majuscule, avant qu'il ne soit affiché.

Si vous avez un contrôleur, AppML enverra l'objet d'application ($appml) au contrôleur, pour chaque action importante.

L'une des propriétés de l'application est un message ($appml.message), décrivant l'état de l'application.

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.

Les messages sont expliqués dans le chapitre suivant.