Charger des données depuis une route Total.js avec AJAX
AJAX('METHOD /url/', [data], callback) envoie une requête et appelle le callback avec la réponse. Les réponses JSON sont analysées pour vous. Combinez-le avec SET() et tout ui-bind ou composant lié au chemin se met à jour tout seul.
exports.install = function() {
ROUTE('GET /api/hello/', hello);
};
function hello($) {
$.json({ message: 'Hello from Total.js' });
}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jComponent</title>
<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>
<p><ui-bind path="greeting" config="text:value">Loading...</ui-bind></p>
<script>
AJAX('GET /api/hello/', function(response) {
SET('greeting', response.message);
});
</script>
</body>
</html> Essayez-le dans votre navigateur
Modifiez l'exemple et exécutez-le. Il s'exécute dans un cadre isolé de votre navigateur ; rien n'est envoyé à notre serveur.
Aperçu