Here is the code I am working with. The console log is showing me the onFilterChange method is being called twice (once for each function definition in the "select tags). I'm guessing I need to change "event: { change: onFilterChange }" in the data-bind, but I'm not familiar with Knockout.js, and we don't support ES6, so no using "()=>onFilterChange" which has worked for me in the past:
function ProductionsView() {
var self = this;
self.showDateFilter = ko.observable(true);
self.showCategoryFilter = ko.observable(true);
self.showDateFilter = ko.observable(true);
self.onFilterChange = function(data,event) {
if (self.init()) {
self.doFilter();
console.log("onFilterChange was called. This is happening twice on page load before interacting with the filter. ");
}
};
self.doFilter = function() {
var df = self.selectedDateFilter();
var cf = self.selectedCatFilter();
if (typeof cf == 'undefined' || typeof df == 'undefined') {
return;
}
// these are all defined as observables too, but not important since it's the "onFilterChange" that I don't want to call
self.amFiltering("All" !== df || "All" !== cf);
self.allCatAndDate("All" === df && "All" === cf);
self.offset(0);
self.doAjaxSearchFiltered(true);
};
}
var pv = new ProductionsView();
ko.applyBindings(pv);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div data-bind="visible: showDateFilterBar()">
<select data-bind="
value: selectedCatFilter,
event: { change: onFilterChange },
visible: showCategoryFilter()
">
<option value="all">all</option>
<option value="cat1">cat1</option>
</select>
<select data-bind="
value: selectedDateFilter,
event: { change: onFilterChange },
visible: showDateFilter()
">
<option value="all">all</option>
<option value="date1">date1</option>
</select>
</div>