Support dynamic functions for computed annotations.

Alternate take for #3299
This commit is contained in:
herr kaste 2016-02-01 23:31:03 +01:00
parent de039f8603
commit 3f1bc4e33a
3 changed files with 73 additions and 2 deletions

View File

@ -145,8 +145,17 @@ TODO(sjmiles): this module should produce either syntactic metadata
for (var k=0; k<b.parts.length; k++) {
var p = b.parts[k];
if (!p.literal) {
p.signature = this._parseMethod(p.value);
if (!p.signature) {
var signature = this._parseMethod(p.value);
if (signature) {
var method = signature.method;
if (this.properties[method]) {
var args = signature.args || [];
args.push(this._parseArg(method));
signature.args = args;
signature.static = false;
}
p.signature = signature;
} else {
p.model = this._modelForPath(p.value);
}
}

View File

@ -582,3 +582,35 @@
})();
</script>
</dom-module>
<dom-module id="x-bind-computed-property">
<template>
<div id="check">[[translate('Hello World.')]]</div>
</template>
<script>
Polymer({
is: 'x-bind-computed-property',
properties: {
translate: {
type: Function,
computed: '_computeTranslateFn(translator)'
},
translator: {
type: Function,
value: function() {
return function(message) {
return 'translated: ' + message;
}
}
},
},
_computeTranslateFn: function(translator) {
return function(message) {
return translator(message);
}
},
});
</script>
</dom-module>

View File

@ -285,6 +285,36 @@ suite('single-element binding effects', function() {
<script>
suite('computed properties', function() {
var el;
setup(function() {
});
teardown(function() {
document.body.removeChild(el);
});
test('x', function() {
el = document.createElement('x-bind-computed-property');
document.body.appendChild(el);
assert.equal(el.$.check.textContent, 'translated: Hello World.');
el.translator = function(message) {
return 'changed: ' + message;
}
assert.equal(el.$.check.textContent, 'changed: Hello World.');
});
});
</script>
<script>
suite('2-way binding effects between elements', function() {
var el;