Skip to content

Commit 3f1bc4e

Browse files
committed
Support dynamic functions for computed annotations.
Alternate take for #3299
1 parent de039f8 commit 3f1bc4e

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/standard/annotations.html

+11-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,17 @@
145145
for (var k=0; k<b.parts.length; k++) {
146146
var p = b.parts[k];
147147
if (!p.literal) {
148-
p.signature = this._parseMethod(p.value);
149-
if (!p.signature) {
148+
var signature = this._parseMethod(p.value);
149+
if (signature) {
150+
var method = signature.method;
151+
if (this.properties[method]) {
152+
var args = signature.args || [];
153+
args.push(this._parseArg(method));
154+
signature.args = args;
155+
signature.static = false;
156+
}
157+
p.signature = signature;
158+
} else {
150159
p.model = this._modelForPath(p.value);
151160
}
152161
}

test/unit/bind-elements.html

+32
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,35 @@
582582
})();
583583
</script>
584584
</dom-module>
585+
586+
<dom-module id="x-bind-computed-property">
587+
<template>
588+
<div id="check">[[translate('Hello World.')]]</div>
589+
</template>
590+
<script>
591+
Polymer({
592+
is: 'x-bind-computed-property',
593+
properties: {
594+
translate: {
595+
type: Function,
596+
computed: '_computeTranslateFn(translator)'
597+
},
598+
translator: {
599+
type: Function,
600+
value: function() {
601+
return function(message) {
602+
return 'translated: ' + message;
603+
}
604+
}
605+
},
606+
},
607+
608+
_computeTranslateFn: function(translator) {
609+
return function(message) {
610+
return translator(message);
611+
}
612+
},
613+
614+
});
615+
</script>
616+
</dom-module>

test/unit/bind.html

+30
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,36 @@
285285

286286
<script>
287287

288+
suite('computed properties', function() {
289+
290+
var el;
291+
292+
setup(function() {
293+
294+
});
295+
296+
teardown(function() {
297+
document.body.removeChild(el);
298+
});
299+
300+
test('x', function() {
301+
el = document.createElement('x-bind-computed-property');
302+
document.body.appendChild(el);
303+
304+
assert.equal(el.$.check.textContent, 'translated: Hello World.');
305+
306+
el.translator = function(message) {
307+
return 'changed: ' + message;
308+
}
309+
310+
assert.equal(el.$.check.textContent, 'changed: Hello World.');
311+
});
312+
313+
});
314+
315+
</script>
316+
317+
<script>
288318
suite('2-way binding effects between elements', function() {
289319

290320
var el;

0 commit comments

Comments
 (0)