Reputation: 740
I need help in subsribing on property in object. I don't know which property name will be and how much, I'm generating an object of properties according to data from the server.
I have code like:
viVm.requestData.ageGroups = {};
var classificators = viVm.requestData.multiTrip() === 1
? viVm.classificators.ageGroups.multiTrip
: viVm.classificators.ageGroups.singleTrip;
classificators().forEach(function (entry, index) {
var entry = ko.toJS(entry);
var code = entry.code;
viVm.requestData.ageGroups[code] = 0;
viVm.requestData.ageGroups[code].subscribe(function (newValue) {
validateAgeGroupValue(newValue, viVm.requestData.ageGroups[code]);
});
});
It sets up property and it's value tom0, but on .subscribe()
step I get an error - viVm.requestData.ageGroups[code].subscribe is not a function
Could enybody explain what I do wrong and maybe offer some variant how to make it work? Thank you.
Upvotes: 0
Views: 176
Reputation: 9530
The reason you are getting this error is because 0 (zero) is not a Knockout observable (function). You are trying to subscribe to an integer.
Try this:
viVm.requestData.ageGroups[code] = ko.observable(0);
viVm.requestData.ageGroups[code].subscribe(function (newValue) {
validateAgeGroupValue(newValue, viVm.requestData.ageGroups[code]);
});
Upvotes: 3