I am trying to call this function "CallAgain()" from click of a button but I am unable to do so. Can someone please advise what I am doing wrong? The second button with data-bind is working fine.
Here is the HTML:
<input id="slid" data-bind="attr: {min: 1, max: MaxPage}, value: CurrPage" type="range" data-highlight="true">
<hr/>
X1=<span data-bind="text: CurrPage"></span></br>
X2=<span data-bind="text: MaxPage"></span>
<hr>
<button onclick="alert(x1);">Call Again</button>
<button data-bind="click: changeCurrent">Change current</button>
And here is JS code:
var x1 = 8,
x2 = 10;
function CallAgain() {
alert("AAA");
var vm = new AppViewModel();
//if ( FirstRun == true ) ko.applyBindings(vm);
x1++;
x2++;
vm.CurrPage(x1);
vm.MaxPage(x2);
vm.changeCurrent();
FirstRun = false;
}
function ViewModel () {
this.CurrPage = ko.observable(x1);
this.MaxPage = ko.observable(x2);
this.CurrPage.subscribe(function (value) {
x1 = value;
});
this.changeCurrent = function () {
x1++;
x2++;
this.CurrPage(x1);
this.MaxPage(x2);
alert( this.CurrPage() );
$('#slid').slider("refresh");
}
};
ko.applyBindings(new ViewModel());
Here is the jsfiddle link for the code
Thanks
Update-1
here is the revised jsfiddle
ChangeCurrent button, which calls changeCurrent() function directly, does update x1 & x2 values in DOM but Call Again button doesn't change them. The second problem is that the slider doesn't update although it has its attributes data-bound.
Any advice please?