Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

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.

Now we mark conditional (as well as || and &&) as dynamicDeps so the
dependencies gets reevaluated as needed.

Fixes #40
  • Loading branch information
arv committed Jul 22, 2014
1 parent 532e3a2 commit d3a45c6
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/polymer-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@
left = getFn(left);
right = getFn(right);

switch (op) {
case '||':
this.dynamicDeps = true;
return function(model, observer, filterRegistry) {
return left(model, observer, filterRegistry) ||
right(model, observer, filterRegistry);
};
case '&&':
this.dynamicDeps = true;
return function(model, observer, filterRegistry) {
return left(model, observer, filterRegistry) &&
right(model, observer, filterRegistry);
};
}

return function(model, observer, filterRegistry) {
return binaryOperators[op](left(model, observer, filterRegistry),
right(model, observer, filterRegistry));
Expand All @@ -297,6 +312,8 @@
consequent = getFn(consequent);
alternate = getFn(alternate);

this.dynamicDeps = true;

return function(model, observer, filterRegistry) {
return test(model, observer, filterRegistry) ?
consequent(model, observer, filterRegistry) :
Expand Down
156 changes: 156 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 @@ -2076,4 +2114,122 @@ suite('PolymerExpressions', function() {
});
});

test('lazy conditional', function(done) {
var div = createTestHtml(
'<template bind=>' +
'{{ a ? b : c }}' +
'</template>');
var count = 0;
var model = {
a: true,
b: 'B',
get c() {
count++;
}
};

recursivelySetTemplateModel(div, model);

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

model.b = 'BB';

}).then(function() {
assert.equal('BB', div.childNodes[1].textContent);
assert.equal(0, count);

done();
});
});

test('lazy conditional 2', function(done) {
var div = createTestHtml(
'<template bind=>' +
'{{ a ? b : c }}' +
'</template>');
var count = 0;
var model = {
a: false,
get b() {
count++;
},
c: 'C'
};

recursivelySetTemplateModel(div, model);

then(function() {
assert.equal('C', div.childNodes[1].textContent);
assert.equal(0, count);

model.c = 'CC';

}).then(function() {
assert.equal('CC', div.childNodes[1].textContent);
assert.equal(0, count);

done();
});
});

test('lazy or', function(done) {
var div = createTestHtml(
'<template bind=>' +
'{{ a || b }}' +
'</template>');
var count = 0;
var model = {
a: 'A',
get b() {
count++;
},
};

recursivelySetTemplateModel(div, model);

then(function() {
assert.equal('A', div.childNodes[1].textContent);
assert.equal(0, count);

model.a = 'AA';

}).then(function() {
assert.equal('AA', div.childNodes[1].textContent);
assert.equal(0, count);

done();
});
});

test('lazy and', function(done) {
var div = createTestHtml(
'<template bind=>' +
'{{ a && b }}' +
'</template>');
var count = 0;
var model = {
a: false,
get b() {
count++;
},
};

recursivelySetTemplateModel(div, model);

then(function() {
assert.equal('false', div.childNodes[1].textContent);
assert.equal(0, count);

model.a = 0;

}).then(function() {
assert.equal('0', div.childNodes[1].textContent);
assert.equal(0, count);

done();
});
});

});

0 comments on commit d3a45c6

Please sign in to comment.