Reputation: 6045
I do know Subscribe
wont fire onload (Init) as per documentation .
But what i have here is bit different and let me try explaining my scenario
I am having a dropdownlist and set of other controls in my create page . Dropdwonlist having a set of listitems when i select something susbscribe
will fire (i subscribed dropdown selected value) and inside subscirbe i will do a ajax call and fill the data in remaining controls and i can MODIFY
data in other controls which came based on dropdwon selection and finally i will save .
Html:
<select data-bind="options:CtList,optionsValue:'Value',optionsText:'Text',optionsCaption:'--Select--',value:CSelected">
Till this point everything works so great but when i take the created id
and come to the page i saved before with modifications those are gone and i can see values which are related to dropdwon selection on my page subscribe effect
.
View Model Code:
self.CSelected = ko.observable("");
self.CSelected.subscribe(function (selected) {
if(selected != undefined)
{ //make a ajax call and load vlaues on success }
});
Truly thats how it tend to happen on OnLoad
in this scenario as there is a value for dropdown (i saved dropdown selected value and loading back) .
Any way to restrict the subscribe
firing on load if i have a value but it should fire when i change selection . If the same thing can be done using any better approach do enlighten me .
I got this working but i am not satisfied still iam looking for perfect solution using KnockOut .
My code:
var loadcount =0;
self.CSelected.subscribe(function (selected) {
if(selected != undefined) {
if(loadcount >0)
{//make a ajax call and load vlaues on success }
loadcount++;
}
else
{loadcount++;}
});
Upvotes: 0
Views: 172
Reputation: 47978
Make the subscribe after filling in the CSelected
saved value, not before.
self.CSelected = ko.observable("");
...
// fill CSelected
self.CSelected(savedValue);
...
self.CSelected.subscribe(function (selected) {
if(selected != undefined)
{ //make a ajax call and load vlaues on success }
});
Upvotes: 1