I'm having trouble using a foreach binding over a list of functions. I want the inner context to be the function itself, but instead, the context is bound to the result of invoking the function. Here's a simple example that illustrates this: (JSFiddle)
JS:
ko.applyBindings({
list: [
function() {
return "hodor";
}
]
});
HTML:
<!-- ko foreach: list -->
<span data-bind="text: typeof $data"></span>
<span data-bind="text: $data"></span>
<br />
<!-- /ko -->
Output is: "string foo" but I would like it to be "function function () { return "foo"; }"
As a workaround I can do $parent.list[$index()]
, but that's really ugly, and I'd like to avoid that syntax, if possible.
(For explanation in my real usage, these aren't simple functions but functions that I've attached extra properties to that I want to reference, but I can't reference them because knockout is invoking them)
EDIT:
It appears that there's a KO version issue here. Before 3.0, it wasn't an issue, the context was always bound to the function not the value. On 3.0, using $rawData
would give the function, not that value, but on 3.2, it doesn't, which is what I'm trying to use.
I've updated the above fiddle to use 3.2, and here's a fiddle for each version, including $rawData
(except in 2.3 where $rawData doesn't exist)
Knockout 2.3 fiddle: $rawData
not needed
Knockout 3.0 fiddle: $rawData
works
Knockout 3.2 fiddle: $rawData
doesn't work.
Could this be a KO 3.2 bug?
$data
work on both 3.0 and 3.2, but not in 2.3. – Matt Burland Oct 29 '14 at 18:12$parent.list[$index()]
workaround, which always works, than wrap my functions again, personally. – Retsam Oct 29 '14 at 18:16