Yes. Subscribe has never really worked for me. In this particular case, however, I've switched to a computed function that seems to fire on it's own without the need for eventhandlers to call the function. And it would seem that it doesn't need a subscribe either, because it works just fine without it. The computed function has if-conditions that check if myObservable() has any value and then it runs some code, else it just returns.
The background is that I'm building a filterfunction and I had some issues with making it work with multiple variables. I took some detours with making new lists based on the filters and then adding the next filter to the new list, but after some deliberation with my colleague we managed to fix it without the extra lists. Here is the function:
self.filterEmpl = ko.computed(function () {
var filteredList = self.employeeList();
if (self.departmentFilter()) {
filteredList = ko.utils.arrayFilter(filteredList, function (employee) {
var departmentFilter = self.departmentFilter().toLowerCase();
return employee;
};
});
}
if (self.officeFilter()) {
filteredList = ko.utils.arrayFilter(filteredList, function (employee) {
var officeFilter = self.officeFilter().toLowerCase();
if (employee.Office.OfficeName != null && employee.Office.OfficeName.toLowerCase().indexOf(officeFilter) >= 0) {
return employee;
};
});
}
if (self.positionFilter()) {
filteredList = ko.utils.arrayFilter(filteredList, function (employee) {
if (employee.Position != null && employee.Position.toLowerCase().indexOf(self.positionFilter().toLowerCase()) >= 0) {
return employee;
};
});
}
return filteredList;
});