Passing values to ko.computed in Knockout JS
asp.net-mvc-4, javascript, knockout.js
Solution
Each firm really should be containing a list of clients, but you could use a regular function I think and pass it the firm:
self.getClientsForFirm = function (firm) {
return ko.utils.arrayFilter(self.Clients(), function (item) {
var fId = item.FirmId();
return (fId === firm.Id());
});
});
Then in html, $data is the current model, in your case the firm:
<div data-bind="foreach: $root.getClientsForFirm($data)">
Problem
I've been working for a bit with MVC4 SPA, with knockoutJs, My problem is I want to pass a value to a ko.computed. Here is my code. ``` <div data-bind="foreach: firms"> <fieldset> <legend><span data-bind="text: Name"></span></legend> <div data-bind="foreach: $parent.getClients"> <p> <span data-bind="text: Name"></span> </p> </div> </fieldset> </div> self.getClients = ko.computed(function (Id) { var filter = Id; return ko.utils.arrayFilter(self.Clients(), function (item) { var fId = item.FirmId(); return (fId === filter); }); }); ``` I simply want to display the Firmname as a header, then show the clients below it. The function is being called, but Id is undefined (I've tried with 'Firm' as well), if I change: ``` var filter = id; TO var filter = 1; ``` It works fine, So... How do you pass a value to a ko.computed? It doesn't need to be the Id, it can also be the Firm object etc.