JSFiddle - React, Tailwind, and code Playground
by Rafael Eyng
HTML
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"></script>
<button data-bind="click: incrementNum.bind($root, num1)">Increment</button>
<p data-bind="text: num1"></p>
<button data-bind="click: decrementNum.bind($root, num2)">Decrement</button>
<p data-bind="text: num2"></p>
JavaScript
var increment = function (observable) {
observable(observable() + 1);
};
var decrement = function (observable) {
observable(observable() - 1);
};
var MyViewModel = function () {
this.num1 = ko.observable(0);
this.num2 = ko.observable(0);
this.incrementNum = function (observable) {
// passing the observable by reference to the helper function
increment(observable);
}
this.decrementNum = function (observable) {
decrement(observable);
}
}
ko.applyBindings(new MyViewModel());