@@ -14,6 +14,8 @@ Within a `<polymer-element>` you can use {{site.project_title}}'s [expression
14
14
library] ( https://github.com/polymer/polymer-expressions ) to write inline expressions,
15
15
named scopes, and iterative markup, anywhere {%raw%}` {{ `   ; ` }} ` {%endraw%} bindings are used.
16
16
17
+ Expressions can also be used to define [ computed properties] ( /docs/polymer/polymer.html#computed-properties ) .
18
+
17
19
## Expression syntax
18
20
19
21
{{site.project_title}} supports expressions in {%raw%}` {{}} ` {%endraw%} with a strict
@@ -45,27 +47,33 @@ Expressions support the following subset of JavaScript:
45
47
| Grouping (parenthesis) | ` (a + b) * (c + d) ` |
46
48
| Literal values | numbers, strings, ` null ` , ` undefined ` | Escaped strings and non-decimal numbers are not supported. |
47
49
| Array & Object initializers | ` [foo, 1] ` , ` {id: 1, foo: bar} ` |
50
+ | Function | ` reverse(my_list) ` | The expression's value is the return value of the function. The function's arguments are observed for changes, and the expression is re-evaluated whenever one of the arguments changes.
48
51
{: .first-col-nowrap .responsive-table .expressions-table }
49
52
53
+
50
54
In addition to the JavaScript portion, an expression can also include one or more _ filters_ , which
51
55
modify the output of the JavaScript expression. See [ Filtering expressions] ( #filters ) for
52
56
information.
53
57
54
58
## Evaluating expressions
55
59
56
- Expressions are parsed when they're within a double-mustache binding:
60
+ Expressions are parsed when they're within a binding or computed property declaration :
57
61
58
- <pre class =" prettyprint " >
59
- {%raw%}{{ <var >expression</var > }} {%endraw%}
62
+ <pre class =" nocode " >
63
+ {%raw%}< b >{{</ b > <var >expression</var > < b >}}</ b > {%endraw%}
60
64
</pre >
61
65
62
- or a one-time binding:
66
+ <pre class =" nocode " >
67
+ <b >[[</b > <var >expression</var > <b >]]</b >
68
+ </pre >
63
69
64
- <pre class =" prettyprint " >
65
- [[ <var >expression</var > ]]
70
+ <pre class =" nocode " >
71
+ <b >computed: {</b >
72
+ <var >property_name</var ><b >: '</b ><var >expression</var ><b >'
73
+ }</b >
66
74
</pre >
67
75
68
- The value of the expression is evaluated and the result inserted as the value of the binding:
76
+ The value of the expression is evaluated. In a binding, the result inserted as the value of the binding:
69
77
70
78
{% raw %}
71
79
<div >Jill has {{daughter.children.length + son.children.length}} grandchildren</div >
@@ -75,8 +83,9 @@ may result in:
75
83
76
84
<div>Jill has 100 grandchildren</div>
77
85
78
- For standard (double-mustache) bindings, the expression is re-evaluated whenever the value of one or
79
- more paths in the expression changes.
86
+ For standard (double-mustache) bindings and computed properties, the
87
+ expression is re-evaluated whenever the value of one or more paths
88
+ in the expression changes.
80
89
81
90
## Expression scopes {#expression-scopes}
82
91
@@ -85,6 +94,8 @@ are visible. The expressions in `bind`, `repeat` or `if` attributes are evaluate
85
94
the parent template. For an element's outermost template, paths and identifiers are
86
95
interpreted relative to the element itself (so ` this.prop ` is available as {%raw%}` prop ` {%endraw%}).
87
96
97
+ For computed properties, the scope of an expression is always the element itself.
98
+
88
99
Templates that don't include ` bind ` or ` repeat ` share the current scope.
89
100
90
101
A ` bind ` or ` repeat ` without an expression is the same as using an expression that
@@ -244,6 +255,8 @@ The code for the `toFixed` filter could look like this:
244
255
return Number(value).toFixed(precision);
245
256
}
246
257
258
+ The parameters passed to a filter are observed for changes.
259
+
247
260
#### Chaining filters
248
261
249
262
You can also chain filters, passing the output of one filter to another:
0 commit comments