@@ -11,7 +11,7 @@ subtitle: Data-binding
11
11
12
12
13
13
Within a ` <polymer-element> ` you can use {{site.project_title}}'s [ expression
14
- library] ( https://github.com/polymer/polymer-expressions ) to write inline expressions,
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
17
Expressions can also be used to define [ computed properties] ( /docs/polymer/polymer.html#computed-properties ) .
@@ -47,7 +47,7 @@ Expressions support the following subset of JavaScript:
47
47
| Grouping (parenthesis) | ` (a + b) * (c + d) ` |
48
48
| Literal values | numbers, strings, ` null ` , ` undefined ` | Escaped strings and non-decimal numbers are not supported. |
49
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.
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.
51
51
{: .first-col-nowrap .responsive-table .expressions-table }
52
52
53
53
@@ -83,29 +83,29 @@ may result in:
83
83
84
84
<div>Jill has 100 grandchildren</div>
85
85
86
- For standard (double-mustache) bindings and computed properties, the
87
- expression is re-evaluated whenever the value of one or more paths
86
+ For standard (double-mustache) bindings and computed properties, the
87
+ expression is re-evaluated whenever the value of one or more paths
88
88
in the expression changes.
89
89
90
90
## Expression scopes {#expression-scopes}
91
91
92
92
Expressions are evaluated based on the current _ scope_ , which defines which identifiers and paths
93
93
are visible. The expressions in ` bind ` , ` repeat ` or ` if ` attributes are evaluated in the scope of
94
- the parent template. For an element's outermost template, paths and identifiers are
94
+ the parent template. For an element's outermost template, paths and identifiers are
95
95
interpreted relative to the element itself (so ` this.prop ` is available as {%raw%}` prop ` {%endraw%}).
96
96
97
97
For computed properties, the scope of an expression is always the element itself.
98
98
99
99
Templates that don't include ` bind ` or ` repeat ` share the current scope.
100
100
101
- A ` bind ` or ` repeat ` without an expression is the same as using an expression that
101
+ A ` bind ` or ` repeat ` without an expression is the same as using an expression that
102
102
specifies the current scope.
103
103
104
104
### Nested scoping rules {#nested-scoping-rules}
105
105
106
106
If a ` <template> ` using a named scope contains child ` <template> ` s,
107
107
all ancestor scopes are visible, up-to and including the first ancestor ** not** using a named scope. For example:
108
-
108
+
109
109
{% raw %}
110
110
<template >
111
111
<!-- outermost template -- element's properties available -->
@@ -116,7 +116,7 @@ all ancestor scopes are visible, up-to and including the first ancestor **not**
116
116
<template bind =" {{contact.address}} " >
117
117
<!-- only properties of address are available -->
118
118
<template bind =" {{streetAddress as streetAddress}} " >
119
- <!-- streetAddress.* and properties of address are available.
119
+ <!-- streetAddress.* and properties of address are available.
120
120
NOT organization.* or contact.* -->
121
121
</template >
122
122
</template >
@@ -133,7 +133,7 @@ In other words:
133
133
## Filtering expressions {#filters}
134
134
135
135
Filters can be used to modify the output of expressions. {{site.project_title}} supports several
136
- default filters for working with data. They're used in bindings by piping an input expression
136
+ default filters for working with data. They're used in bindings by piping an input expression
137
137
to the filter:
138
138
139
139
<pre class =" prettyprint " >
@@ -162,17 +162,17 @@ properties are observed for changes.
162
162
163
163
The ` tokenList ` filter is useful for binding to the ` class ` attribute. It allows you
164
164
to dynamically set/remove class names based on the object passed to it. If the object
165
- key is truthy, the name will be applied as a class.
165
+ key is truthy, the name will be applied as a class.
166
166
167
167
For example:
168
168
169
169
{% raw %}
170
- <div class =" {{ {active: user.selected, big: user.type == 'super'} | tokenList}} " >
170
+ <div class =" {{ {active: user.selected, big: user.type == 'super'} | tokenList}} " >
171
171
{% endraw %}
172
172
173
173
results in the following if ` user.selected == true ` and ` user.type == 'super' ` :
174
174
175
- <div class="active big">
175
+ <div class="active big">
176
176
177
177
### styleObject
178
178
@@ -198,7 +198,7 @@ use the `styleObject` filter to transform it into the appropriate format.
198
198
{% endraw %}
199
199
200
200
In this examples ` styles ` is an object of the form ` {color: 'red', background: 'blue'} ` , and
201
- the output of the ` styleObject ` filter is a string of CSS declarations (for example,
201
+ the output of the ` styleObject ` filter is a string of CSS declarations (for example,
202
202
` "color: 'red'; background: 'blue'" ` ).
203
203
204
204
### Writing custom filters {#custom-filters}
@@ -265,3 +265,64 @@ You can also chain filters, passing the output of one filter to another:
265
265
{{myNumber | toHex | toUpperCase}}
266
266
{% endraw %}
267
267
268
+ ### Writing global filters
269
+
270
+
271
+ Although Polymer supports adding [ custom filters] ( #custom-filters ) to the element prototype,
272
+ you may want to register global filters which can be reused across multiple elements.
273
+
274
+ This can be achieved by adding a custom filter to the prototype of the
275
+ ` PolymerExpressions ` object.
276
+
277
+ #### Creating global filters
278
+
279
+ Let's create a filter for transforming the letters in any templated string
280
+ to uppercase. Once registered, we will be able to use it as follows:
281
+
282
+ {% raw %}
283
+ {{Hello Polymer | uppercase}}
284
+ {% endraw %}
285
+
286
+ To register ` uppercase ` as a global filter, we add it to the ` PolymerExpressions `
287
+ object and define our filter as a function which operates on the ` input ` supplied.
288
+ In this case the input is the string ` "Hello Polymer" ` . Our final filter is:
289
+
290
+ {% raw %}
291
+ PolymerExpressions.prototype.uppercase = function(input){
292
+ return input.toUpperCase();
293
+ };
294
+ {% endraw %}
295
+
296
+ Earlier we mentioned you can pass parameters to a filter. This is similarly
297
+ supported for global filters. One example is a ` startsWith ` filter that could
298
+ return only those items in an array that begin with a certain letter:
299
+
300
+ {% raw %}
301
+ {{arrayOfFriends | startsWith('M')}}
302
+ {% endraw %}
303
+
304
+ The ` letter ` parameter (and any other parameters we wish to specify) can be defined
305
+ after the initial input as used as you would in any normal function:
306
+
307
+ {% raw %}
308
+ PolymerExpressions.prototype.startsWith = function (input, letter) {
309
+ return input.filter(function(item){
310
+ return item[ 0] === letter;
311
+ });
312
+ };
313
+ {% endraw %}
314
+
315
+ A community-driven [ collection] ( https://github.com/addyosmani/polymer-filters ) of Polymer filters is available if you require further
316
+ inspiration for writing your own global filters.
317
+
318
+ #### Where to include global filters
319
+
320
+ Filters you wish to use across multiple elements can be defined in script and
321
+ imported in at the top of your Polymer element after Polymer has loaded. E.g:
322
+
323
+ {% raw %}
324
+ <link rel =" import " href =" uppercase-filter.html " />
325
+ {% endraw %}
326
+
327
+ Alternatively, you can include filters that will only be used inside a single
328
+ element in script before your Polymer element is registered.
0 commit comments