Un principe fort : MVVM
Principale force : Gestion des dépendances
# Model/account.js
define(["knockout", "sms-service"], function(ko, sms) {
return function account(name, balance) {
var self = this;
self.name = ko.observable(name);
self.balance = ko.observable(balance);
self.status = ko.computed(function() {
return self.balance() >= O ? "positive" : "negative";
});
self.message = ko.computed(function() {
return self.status() === "positive" ?
"vous avez encore de l'argent" :
"arrêtez de dépenser autant !!";
});
self.alerte = ko.computed(function() {
if (self.status() === "negative")
sms.send("Alerte !", "Account balance : " + self.balance());
});
}
}
# View/account.html
<html>
<head>
<script src="knockout.js"></script>
<script src="account.js"></script>
</head>
<body>
...
<label>Balance :
<input data-binding="{value: balance, valueUpdate: 'afterkeydown'}" />
</label>
<p>
Status : <span><% status %></span>
</p>
<p>
Message : <span data-binding="{text: message}"></span>
</p>
...
</body>
</html>
# Controller/bank.html
define(["knockout", "Model/account"],
function(ko, account) {
ko.applyBindings(
new account("Rémi Alvado", 0),
document.querySelector("body")
);
});
80% de Knockout en 7 minutes !