Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

foreach and template bindings evaluate view model expression multiple times #2480

Open
fastfasterfastest opened this issue Apr 19, 2019 · 4 comments

Comments

@fastfasterfastest
Copy link

(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/

@mbest
Copy link
Member

mbest commented Apr 20, 2019

Looks like this has been the case since 3.0.0.

@fastfasterfastest
Copy link
Author

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.

@mbest
Copy link
Member

mbest commented Apr 22, 2019

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.

@miellaby
Copy link
Contributor

Also see: #2332

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants