I have an observable A that is bound to an UI element. I also have a computed B that depends on A. I have a computed ะก that depends on both A and B. I have a subscription to C
When value in UI element is changed then computed is evaluated twice and subscription is called twice.
I think the reason is that A has two subscriptions:
A: [B, C]
Knockout notifies B about changes in A.
After B was evaluated it notifies C about changes in B
Then it goes back to the start and calls second subscription of A which is C.
Here we have two calls to C.
Is there a way to prevent this?
var viewModel = {
firstName: ko.observable("Andrew"),
lastName: ko.observable("King"),
};
viewModel.fullName = ko.computed(function() {
return viewModel.firstName() + " " + viewModel.lastName();
});
viewModel.user = ko.computed(function() {
return {
fullName: viewModel.fullName(),
lastName: viewModel.lastName()
};
});
viewModel.user.subscribe(function() {
// This is called once if I change first name
// It is called twice if I change last name
});