Trying to get basic Knockout click binding set up, as per the example below:
<button id="btn-a" class="btn" data-bind="css: {'btn-primary':mode() == 'manual'}, click: $root.mode('manual')">Manual</button>
<button id="btn-b" class="btn" data-bind="css: {'btn-primary':mode() == 'automatic'}, click: $root.mode('automatic')">Automatic</button>
<label>MODE: </label><span data-bind="text:mode()"></span>
<script>
$(function () {
var TestModel = function() {
var self = this;
this.mode = ko.observable('manual');
};
var testModel = new TestModel();
window.testModel = testModel;
ko.applyBindings(testModel);
});
Fiddle: http://jsfiddle.net/aq85wk65/
However, running into two issues:
- The binding causes the
mode()
value to start out as 'automatic', even though we explicitly initialize it to 'manual'. - Whenever a button is clicked, the javascript console shows:
Uncaught TypeError: h.apply is not a function