I'm trying to create an HTML form that updates some of its values based on what is selected in a dropdown. The view model looks something like this:
function RoomViewModel() {
var self = this;
self.companyOptions = @Html.Raw({ ... });
self.companyValue = ko.observable().extend({ rateLimit: 5000 });
self.companyInfo = ko.observable();
ko.computed(function() {
if (self.companyValue()) {
$.getJSON('@Html.GenerateActionLinkUrl("GetCompanyAndPlans")', {}, self.companyInfo);
}
});
}
ko.options.deferUpdates = true;
ko.applyBindings(new RoomViewModel());
I then bind my select
dropdown to companyValue
, and if I change the selection a bunch of times, only after 5 seconds does the computed
kick in and display the currently-selected value. This comes close to doing what I want, but the one problem is that the first time you change the dropdown, you shouldn't have to wait 5 seconds - it should make the JSON call immediately. The rate limiting is to stop further JSON requests in between the first change and 5 seconds later. So how can I make it immediately do the JSON request and update for the first change?