Handling templates with if and ifnot bindings
You have learned how to show and hide templates with the power of jQuery and Bootstrap. This is quite good because you can use this technique with any framework you want. The problem with this type of code is that since jQuery is a DOM manipulation library, you need to reference elements to manipulate them. This means you need to know over which element you want to apply the action. Knockout gives us some bindings to hide and show elements depending on the values of our view-model. Let's update the show
and hide
methods and the templates.
Add both the control variables to your view-model and expose them in the return
statement.
var visibleCatalog = ko.observable(true); var visibleCart = ko.observable(false);
Now update the show
and hide
methods:
var showCartDetails = function () { if (cart().length > 0) { visibleCart(true); } }; var hideCartDetails = function () { visibleCart(false); }; var showOrder = function () { visibleCatalog...