How can i change value of observable inside of subscribe function? For example
JS Model:
function Model(){
self = this;
self.Test = ko.observable(2);
self.Test.subscribe(function (){
if (/**Some bool expression**/) {
self.Test(2);
}
});
}
HTML:
<input type="radio" value="1" data-bind="checked:Test" />
<input type="radio" value="2" data-bind="checked:Test" />
<input type="radio" value="3" data-bind="checked:Test" />
By default second radio input is checked. After I clicked on first radio, both first and second is selected.
UPDATED: It is happens when I include both jQuery and knockout. If remove jquery then all is Ok. But it is just test page. In real project I need to use jQuery in some places, and in this case I can not remove it.
Sample. Source of test page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="knockout-2.1.0.js"></script>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript">
function Model(){
self = this;
self.Test = ko.observable(2);
self.Test.subscribe(function (){
if (true) {
self.Test(2);
}
});
}
</script>
</head>
<body>
<input type="radio" value="1" data-bind="checked:Test" />
<input type="radio" value="2" data-bind="checked:Test" />
<input type="radio" value="3" data-bind="checked:Test" />
</body>
<script type='text/javascript'>
ko.applyBindings(new Model());
</script>
</html>
name
attribute, which will keep them grouped, although KO will still keep them in sync anyways. – Gramme