I have a computed observable that uses ko.toJS() to post values (inner observables) from the view model. One of the inner observables is bound to a textbox. How can I prevent changes to the textbox from automatically triggering the computed observable (i.e. the postback)?
function viewModel() {
var self = this;
self.SearchParams = {
OrderStatusID: ko.observable(),
OrderNumber: ko.observable(), // I don't want this observable to trigger the postback
OrderBy: ko.observable(),
OrderByDirection: ko.observable(),
PageIndex: ko.observable(),
PageSize: ko.observable()
};
// This computed observable automatically subscribes to every
// observable in self.SearchParams. How can I ignore changes to
// self.SearchParams.OrderNumber?
ko.computed(function () {
NS.post({
url: '/SomeUrl',
data: ko.toJS(self.SearchParams)
});
}).extend({ throttle: 1 });
}
Knockout 2.2+ includes a
peekfunction to access observables without subscribing. So you could do the following:If you’re stuck on Knockout 2.1.0, you can extend observables manually to add
peeklike this (thanks Ryan Niemeyer):And then use
backdoorObservableforOrderNumber: