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

Commit

Permalink
Add support for object[expression]
Browse files Browse the repository at this point in the history
  • Loading branch information
arv committed Aug 15, 2013
1 parent 8489234 commit 1c31d44
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/polymer-expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@
},

createMemberExpression: function(accessor, object, property) {
if (accessor === '[') {
object = getFn(object);
property = getFn(property);
return function(values) {
return object(values)[property(values)];
};
}
return new IdentPath(this.deps, property.name, object);
},

Expand Down
87 changes: 87 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,4 +610,91 @@ suite('PolymerExpressions', function() {
assert.equal(errors[0][0],
'Invalid expression syntax: bar | upperCase + 42');
});

test('Member lookup with constant expressions', function() {
var div = createTestHtml(
'<template bind>' +
'{{ array[0] }} {{ object["a"] }}' +
'</template>');
var model = {
array: ['a', 'b'],
object: {
a: 'A'
}
};

recursivelySetTemplateModel(div, model);
Platform.performMicrotaskCheckpoint();
assert.equal('a A', div.childNodes[1].textContent);

model.array = ['c', 'd'];
Platform.performMicrotaskCheckpoint();
assert.equal('c A', div.childNodes[1].textContent);

model.object = {a: 'E'};
Platform.performMicrotaskCheckpoint();
assert.equal('c E', div.childNodes[1].textContent);
});

test('Member lookup', function() {
var div = createTestHtml(
'<template bind>' +
'{{ array[index] }} {{ object[key] }}' +
'</template>');
var model = {
array: ['a', 'b'],
index: 0,
object: {
a: 'A',
b: 'B'
},
key: 'a'
};

recursivelySetTemplateModel(div, model);
Platform.performMicrotaskCheckpoint();
assert.equal('a A', div.childNodes[1].textContent);

model.index = 1;
Platform.performMicrotaskCheckpoint();
assert.equal('b A', div.childNodes[1].textContent);

model.key = 'b';
Platform.performMicrotaskCheckpoint();
assert.equal('b B', div.childNodes[1].textContent);

model.array = ['c', 'd'];
Platform.performMicrotaskCheckpoint();
assert.equal('d B', div.childNodes[1].textContent);

model.object = {
a: 'C',
b: 'D'
};
Platform.performMicrotaskCheckpoint();
assert.equal('d D', div.childNodes[1].textContent);
});

test('Member lookup nested', function() {
var div = createTestHtml(
'<template bind>' +
'{{ object[array[index]] }}' +
'</template>');
var model = {
array: ['a', 'b'],
index: 0,
object: {
a: 'A',
b: 'B'
}
};

recursivelySetTemplateModel(div, model);
Platform.performMicrotaskCheckpoint();
assert.equal('A', div.childNodes[1].textContent);

model.index = 1;
Platform.performMicrotaskCheckpoint();
assert.equal('B', div.childNodes[1].textContent);
});
});

0 comments on commit 1c31d44

Please sign in to comment.