chap02 -> Creating templates -> The cart-widget template

This commit is contained in:
Jason Zhu 2022-04-20 21:30:56 +10:00
parent b299143ee2
commit b1fc664fe9
2 changed files with 21 additions and 1 deletions

View File

@ -127,7 +127,10 @@
</div> <!-- /.modal-diaglog -->
</div> <!-- /.modal -->
</script>
<script type="text/html" id="cart-widget"></script>
<script type="text/html" id="cart-widget">
Total Items: <span data-bind="text:totalItems"></span>
Price: <span data-bind="text:grandTotal"></span>
</script>
<script type="text/html" id="cart-item"></script>
<script type="text/html" id="cart"></script>
<script type="text/html" id="order"></script>

View File

@ -65,6 +65,23 @@ var vm = (function () {
}
cart(tmpCart);
};
var totalItems = ko.computed(function() {
var tmpCart = cart();
var total = 0;
tmpCart.forEach(function(item) {
total += parseInt(item.units(), 10);
});
return total;
})
var grandTotal = ko.computed(function() {
var tmpCart = cart();
var total = 0;
tmpCart.forEach(function(item) {
total += (item.units() * item.product.price());
});
return total;
})
return {
// first chapter