Skip to content

Commit

Permalink
Fix issue with conditional expressions
Browse files Browse the repository at this point in the history
For conditionals we failed to setup the observer for both branches
as required. This lead to us only observing either the true or false
branch but not both.

Fixes googlearchive#40
  • Loading branch information
arv committed Jul 22, 2014
1 parent 532e3a2 commit d75d064
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/polymer-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,17 @@
consequent = getFn(consequent);
alternate = getFn(alternate);

// We need to ensure we set up observers on both branches the first time.
var firstTime = true;

return function(model, observer, filterRegistry) {
if (firstTime) {
var t = test(model, observer, filterRegistry);
var c = consequent(model, observer, filterRegistry);
var a = alternate(model, observer, filterRegistry);
firstTime = false;
return t ? c : a;
}
return test(model, observer, filterRegistry) ?
consequent(model, observer, filterRegistry) :
alternate(model, observer, filterRegistry);
Expand Down
43 changes: 43 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,44 @@ suite('PolymerExpressions', function() {
});
});

test('Expressions Conditional 2', function(done) {
var div = createTestHtml(
'<template bind>' +
'{{ checked ? a : b }}' +
'</template>');
var model = {
checked: false,
a: 'A',
b: 'B'
};

recursivelySetTemplateModel(div, model);

then(function() {
assert.strictEqual('B', div.childNodes[1].textContent);

model.checked = true;

}).then(function() {
assert.strictEqual('A', div.childNodes[1].textContent);

model.a = 'AAA';
}).then(function() {
assert.strictEqual('AAA', div.childNodes[1].textContent);

model.checked = false;

}).then(function() {
assert.strictEqual('B', div.childNodes[1].textContent);

model.b = 'BBB';
}).then(function() {
assert.strictEqual('BBB', div.childNodes[1].textContent);

done();
});
});

test('Expressions Literals', function(done) {
var div = createTestHtml(
'<template bind>' +
Expand Down Expand Up @@ -1794,6 +1832,11 @@ suite('PolymerExpressions', function() {
assert.isTrue(getExpression('a[a.b] + d[e].f').dynamicDeps);
assert.isTrue(getExpression('a[b + c]').dynamicDeps);
assert.isTrue(getExpression('a[b + 2].c + d').dynamicDeps);

assert.isFalse(getExpression('a ? b : c').dynamicDeps);
assert.isTrue(getExpression('a[a.b] ? b : c').dynamicDeps);
assert.isTrue(getExpression('a ? b[b.c] : c').dynamicDeps);
assert.isTrue(getExpression('a ? b : c[c.d]').dynamicDeps);
});

// https://github.com/Polymer/polymer-expressions/issues/24
Expand Down

0 comments on commit d75d064

Please sign in to comment.