I am trying to do some refactoring on a knockout view model. Starting with separating some view logic from the model object.
I was wondering if there is a way to pass a value back when using a visible binding?
My current situation I am in a Foreach loop. I wanted to pass back either the object Knockout is iterating over, to the method that I call on the visible binding:
data-bind="visible: SomeMethod"
to
data-bind="visible: SomeMethod(object)"
The method is obviously a ko.computed function, and I have tried to pass back the value to it and been unsuccessful. Its always undefined.
var SomeMethod = ko.computed(function() {
.....
}
Any ideas if this is even possible?
Knockout executes binding within a computed observable to track dependencies. I am not sure of your exact objective, but you can just use a normal function in your case.
Depending on what you want to pass in, you will likely want to use
$datain your binding like:The other choice that is used more commonly is to actually place a computed on each item that returns the appropriate value. If the value that you are passing to the function is represented on the item itself, then you should be able to create a computed to cleanly represent its value.