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

Commit

Permalink
polymer-expressions: oneTime
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Dec 18, 2013
1 parent 5edddb4 commit d7c089e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/polymer-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
return;
}

return function(model, node) {
var binding = expression.getBinding(model, filterRegistry);
return function(model, node, oneTime) {
var binding = expression.getBinding(model, filterRegistry, oneTime);
if (expression.scopeIdent && binding) {
node.polymerExpressionScopeIdent_ = expression.scopeIdent;
if (expression.indexIdent)
Expand Down Expand Up @@ -368,8 +368,22 @@
}

Expression.prototype = {
getBinding: function(model, filterRegistry) {
getBinding: function(model, filterRegistry, oneTime) {
var paths = this.paths;
if (oneTime) {
var values;

if (paths.length == 1) {
values = paths[0].getValueFrom(model);
} else if (paths.length > 1) {
values = [];
for (var i = 0; i < paths.length; i++)
values[i] = paths[i].getValueFrom(model);
}

return this.getValue(values, filterRegistry, model);
}

if (!paths.length) {
// only literals in expression.
return new ConstantObservable(this.getValue(undefined, filterRegistry,
Expand Down
63 changes: 63 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,23 @@ suite('PolymerExpressions', function() {
assertLacksClass(target, 'foo');
});


test('ClassName Singular Static', function() {
var div = createTestHtml(
'<template bind><div class="[[ {foo: bar} | tokenList ]]">' +
'</div></template>');
var model = {bar: 1};
recursivelySetTemplateModel(div, model);
Platform.performMicrotaskCheckpoint();

var target = div.childNodes[1];
assertHasClass(target, 'foo');

model.bar = 0;
Platform.performMicrotaskCheckpoint();
assertHasClass(target, 'foo');
});

test('ClassName Multiple', function() {
var div = createTestHtml(
'<template bind>' +
Expand All @@ -154,6 +171,31 @@ suite('PolymerExpressions', function() {
assertHasClass(target, 'boo');
});

test('ClassName Multiple - static', function() {
var div = createTestHtml(
'<template bind>' +
'<div class="[[ {foo: bar, baz: bat > 1, boo: bot.bam} ' +
'| tokenList ]]">' +
'</div></template>');
var model = {bar: 1, bat: 1, bot: { bam: 1 }};
recursivelySetTemplateModel(div, model);
Platform.performMicrotaskCheckpoint();

var target = div.childNodes[1];
assert.strictEqual('foo boo', target.className);
assertHasClass(target, 'foo');
assertLacksClass(target, 'baz');
assertHasClass(target, 'boo');

model.bar = 0;
model.bat = 2;
Platform.performMicrotaskCheckpoint();
assert.strictEqual('foo boo', target.className);
assertHasClass(target, 'foo');
assertLacksClass(target, 'baz');
assertHasClass(target, 'boo');
});

test('tokenList', function() {
var div = createTestHtml(
'<template bind>' +
Expand All @@ -175,6 +217,27 @@ suite('PolymerExpressions', function() {
assert.strictEqual('bar bot', target.className);
});

test('tokenList - static', function() {
var div = createTestHtml(
'<template bind>' +
'<div class="[[ object | tokenList ]]">' +
'</div></template>');

var model = {
object: {bar: 1, bat: 1, bot: {bam: 1}}
};
recursivelySetTemplateModel(div, model);
Platform.performMicrotaskCheckpoint();

var target = div.childNodes[1];
assert.strictEqual('bar bat bot', target.className);

model.object = {bar: 1, bot: 1};
Platform.performMicrotaskCheckpoint();

assert.strictEqual('bar bat bot', target.className);
});

test('styleObject', function() {
// IE removes invalid style attribute values so we use xstyle in this test.

Expand Down

0 comments on commit d7c089e

Please sign in to comment.