chap3 -> Custom bindings -> The toggle binding

chap3
Jason Zhu 2022-04-30 18:56:38 +10:00
parent 5f833290b6
commit 7c1c4ee8f1
3 changed files with 12 additions and 1 deletions

View File

@ -27,6 +27,7 @@
<script type="text/javascript" src="js/vendors/bootstrap.js"></script>
<script type="text/javascript" src="js/vendors/knockout-3.2.0.js"></script>
<script type="text/javascript" src="js/vendors/koExternalTemplateEngine_all.js"></script>
<script type="text/javascript" src="js/custom/koBindings.js"></script>
<!-- app -->
<script type="text/javascript" src="js/models/Product.js"></script>
<script type="text/javascript" src="js/models/CartProduct.js"></script>

View File

@ -0,0 +1,10 @@
ko.bindingHandlers.toggle = {
init: function (element, valueAccessor) {
var value = valueAccessor();
ko.applyBindingsToNode(element, {
click: function() {
value(!value());
}
});
}
};

View File

@ -9,7 +9,7 @@
</button>
<button
class="btn btn-primary btn-sm"
data-bind="click: showCartDetails, css:{ disabled: cart().length < 1}"
data-bind="toggle:visibleCart, css:{ disabled: cart().length < 1}"
>
Show Cart Details
</button>