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

eq helper called twice #1931

Closed
justinbmeyer opened this issue Sep 17, 2015 · 1 comment
Closed

eq helper called twice #1931

justinbmeyer opened this issue Sep 17, 2015 · 1 comment

Comments

@justinbmeyer
Copy link
Contributor

Related to #1412.

If eq is used like:

{{eq foo.length bar.length}}

If foo.length and bar.length were to change together in a batch, and stay the same, this helper would be called twice and cause unnecessary updating of the DOM.

The solution is to create a can.compute() that represents if .fn() or .inverse() should be called. For example:

var lastValue, curValue,
    options = arguments[arguments.length - 1];

if (arguments.length - 2 <= 0) {
    return options.inverse();
}
var args = arguments;
var callFn = can.compute(function(){
  for (var i = 0; i < args.length - 1; i++) {
    curValue = resolve(args[i]);
    curValue = can.isFunction(curValue) ? curValue() : curValue;

    if (i > 0) {
        if (curValue !== lastValue) {
            return false;
        }
    }
    lastValue = curValue;
  }
  return true;
})


return callFn() ? options.fn() : options.inverse();

This way, eq is only called when the result of callFn changes.

@justinbmeyer
Copy link
Contributor Author

Pull request here: #2060 waiting on merge from @alexisabril

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

No branches or pull requests

2 participants