chap02 -> Creating template -> The cart template

chap2
Jason Zhu 2022-04-20 22:02:20 +10:00
parent a9fdd8d516
commit 7256b15da7
2 changed files with 25 additions and 2 deletions

View File

@ -152,7 +152,17 @@
</div> </div>
</div> </div>
</script> </script>
<script type="text/html" id="cart"></script> <script type="text/html" id="cart">
<button type="button" class="close pull-right" data-bind="click:hideCartDetails">
<span>&times;</span>
</button>
<h1>Cart</h1>
<div data-bind="template: {name: 'cart-item', foreach:cart}" class="list-group"></div>
<div data-bind="template: {name: 'cart-widget'}"></div>
<button class="btn btn-primary btn-sm" data-binid="click:showOrder">
Confirm Order
</button>
</script>
<script type="text/html" id="order"></script> <script type="text/html" id="order"></script>
<script type="text/html" id="finish-order-modal"></script> <script type="text/html" id="finish-order-modal"></script>
<!-- vendor library --> <!-- vendor library -->

View File

@ -66,6 +66,7 @@ var vm = (function () {
cart(tmpCart); cart(tmpCart);
}; };
// The cart-widget template
var totalItems = ko.computed(function() { var totalItems = ko.computed(function() {
var tmpCart = cart(); var tmpCart = cart();
var total = 0; var total = 0;
@ -83,6 +84,7 @@ var vm = (function () {
return total; return total;
}) })
// The cart-item template
var removeFromCart = function (data) { var removeFromCart = function (data) {
var units = data.units(); var units = data.units();
var stock = data.product.stock(); var stock = data.product.stock();
@ -90,6 +92,15 @@ var vm = (function () {
cart.remove(data); cart.remove(data);
} }
// The cart template
var hideCartDetails = function() {
$('#cartContainer').addClass("hidden");
};
var showOrder = function () {
$('#catalogContainer').addClass("hidden");
$('#orderContainer').removeClass("hidden");
}
return { return {
// first chapter // first chapter
searchTerm: searchTerm, searchTerm: searchTerm,
@ -101,7 +112,9 @@ var vm = (function () {
showCartDetails: showCartDetails, showCartDetails: showCartDetails,
totalItems: totalItems, totalItems: totalItems,
grandTotal: grandTotal, grandTotal: grandTotal,
removeFromCart: removeFromCart removeFromCart: removeFromCart,
hideCartDetails: hideCartDetails,
showOrder: showOrder
}; };
})(); })();
ko.applyBindings(vm); ko.applyBindings(vm);