eq helper called twice #1931

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

Comments

Projects
None yet
2 participants
@justinbmeyer
Contributor

justinbmeyer commented Sep 17, 2015

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

This comment has been minimized.

Show comment
Hide comment
@justinbmeyer

justinbmeyer Nov 9, 2015

Contributor

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

Contributor

justinbmeyer commented Nov 9, 2015

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

alexisabril pushed a commit that referenced this issue Nov 9, 2015

alexisabril pushed a commit that referenced this issue Nov 9, 2015

@daffl daffl closed this in #2060 Nov 10, 2015

daffl added a commit that referenced this issue Nov 10, 2015

Merge pull request #2060 from canjs/eq-1931
implementing compute to check for comparisons within #is. fixes #1931
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment