(Compare 3.5.0 Regression: with-binding re-invokes given function call expression instead of using returned value where it was reported that the with
binding evaluates its view model expression multiple times.)
The foreach
binding evaluates its view model expression multiple times.
https://jsfiddle.net/fastfasterfastest/y5pndfku/
The template
binding evaluates its view model expression multiple times.
https://jsfiddle.net/fastfasterfastest/vLt6zsjg/
Looks like this has been the case since 3.0.0.
And you agree it is a bug?
We surely can't have
with: expr
foreach: expr
template: { data: expr }
behave differently when it comes to how (often) knockout evaluates the expression, right? I think knockout either needs to guarantee it evaluates the expression once for all those bindings (and potentially others), or expressly state that the expression may be evaluated multiple times. If the latter, I think #2455 should be re-opened and deemed not-a-bug.
I think it is important that the built-in bindings knockout provides behave consistently.
It's true we haven't previously tried to optimize how many times the binding value is evaluated. Checking the 3.5.0 tests, I found six that fail if I purposely introduce an extra evaluation (see below). But each of these is only using the number of evaluations as a proxy for updates of the binding. I think it makes sense, then, to re-tag #2455 as a feature request and not a true bug.
- Deferred bindings Should leave descendant nodes unchanged if the value is truthy and remains truthy when changed.
- Templating Data binding syntax should permit nested templates, and only bind inner templates once when using getBindingAccessors.
- Templating Data binding syntax should permit nested templates, and only bind inner templates once when using getBindings.
- Native template engine Anonymous templates may be nested.
- Binding: If Should leave descendant nodes unchanged if the value is truthy and remains truthy when changed.
- Binding: Using Should minimize binding updates with nested bindings.
Most helpful comment
And you agree it is a bug?
We surely can't have
with: expr
foreach: expr
template: { data: expr }
behave differently when it comes to how (often) knockout evaluates the expression, right? I think knockout either needs to guarantee it evaluates the expression once for all those bindings (and potentially others), or expressly state that the expression may be evaluated multiple times. If the latter, I think #2455 should be re-opened and deemed not-a-bug.
I think it is important that the built-in bindings knockout provides behave consistently.