Skip to content

Commit 593992a

Browse files
committed
Merge pull request Polymer#573 from Polymer/globalfilters
Adds global filters docs for Polymer#571
2 parents 25a2b1f + b71e01a commit 593992a

File tree

1 file changed

+66
-13
lines changed

1 file changed

+66
-13
lines changed

docs/polymer/expressions.md

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ subtitle: Data-binding
1111

1212

1313
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,
1515
named scopes, and iterative markup, anywhere {%raw%}`{{`&nbsp;`}}`{%endraw%} bindings are used.
1616

1717
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:
4747
| Grouping (parenthesis) | `(a + b) * (c + d)` |
4848
| Literal values | numbers, strings, `null`, `undefined` | Escaped strings and non-decimal numbers are not supported. |
4949
| 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.
5151
{: .first-col-nowrap .responsive-table .expressions-table }
5252

5353

@@ -83,29 +83,29 @@ may result in:
8383

8484
<div>Jill has 100 grandchildren</div>
8585

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
8888
in the expression changes.
8989

9090
## Expression scopes {#expression-scopes}
9191

9292
Expressions are evaluated based on the current _scope_, which defines which identifiers and paths
9393
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
9595
interpreted relative to the element itself (so `this.prop` is available as {%raw%}`prop`{%endraw%}).
9696

9797
For computed properties, the scope of an expression is always the element itself.
9898

9999
Templates that don't include `bind` or `repeat` share the current scope.
100100

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
102102
specifies the current scope.
103103

104104
### Nested scoping rules {#nested-scoping-rules}
105105

106106
If a `<template>` using a named scope contains child `<template>`s,
107107
all ancestor scopes are visible, up-to and including the first ancestor **not** using a named scope. For example:
108-
108+
109109
{% raw %}
110110
<template>
111111
<!-- outermost template -- element's properties available -->
@@ -116,7 +116,7 @@ all ancestor scopes are visible, up-to and including the first ancestor **not**
116116
<template bind="{{contact.address}}">
117117
<!-- only properties of address are available -->
118118
<template bind="{{streetAddress as streetAddress}}">
119-
<!-- streetAddress.* and properties of address are available.
119+
<!-- streetAddress.* and properties of address are available.
120120
NOT organization.* or contact.* -->
121121
</template>
122122
</template>
@@ -133,7 +133,7 @@ In other words:
133133
## Filtering expressions {#filters}
134134

135135
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
137137
to the filter:
138138

139139
<pre class="prettyprint">
@@ -162,17 +162,17 @@ properties are observed for changes.
162162

163163
The `tokenList` filter is useful for binding to the `class` attribute. It allows you
164164
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.
166166

167167
For example:
168168

169169
{% raw %}
170-
<div class="{{ {active: user.selected, big: user.type == 'super'} | tokenList}}">
170+
<div class="{{ {active: user.selected, big: user.type == 'super'} | tokenList}}">
171171
{% endraw %}
172172

173173
results in the following if `user.selected == true` and `user.type == 'super'`:
174174

175-
<div class="active big">
175+
<div class="active big">
176176

177177
### styleObject
178178

@@ -198,7 +198,7 @@ use the `styleObject` filter to transform it into the appropriate format.
198198
{% endraw %}
199199

200200
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,
202202
`"color: 'red'; background: 'blue'"`).
203203

204204
### Writing custom filters {#custom-filters}
@@ -265,3 +265,56 @@ You can also chain filters, passing the output of one filter to another:
265265
{{myNumber | toHex | toUpperCase}}
266266
{% endraw %}
267267

268+
### Writing global filters
269+
270+
271+
Although {{site.project_title}} 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+
To register a global filter, add a custom filter to the prototype of the
275+
`PolymerExpressions` object.
276+
277+
#### Examples
278+
279+
Let's create a filter for transforming the letters in any string to uppercase.
280+
281+
{% raw %}
282+
{{Hello Polymer | uppercase}}
283+
{% endraw %}
284+
285+
To register `uppercase` as a global filter, add it to the prototype for
286+
`PolymerExpressions` object:
287+
288+
{% raw %}
289+
PolymerExpressions.prototype.uppercase = function(input) {
290+
return input.toUpperCase();
291+
};
292+
{% endraw %}
293+
294+
You can also pass parameters to global filters. Here's an example of a `startsWith`
295+
filter that returns only those items in an array that begin with a certain letter:
296+
297+
{% raw %}
298+
{{arrayOfFriends | startsWith('M')}}
299+
{% endraw %}
300+
301+
Filter definition:
302+
303+
{% raw %}
304+
PolymerExpressions.prototype.startsWith = function (input, letter) {
305+
return input.filter(function(item){
306+
return item[0] === letter;
307+
});
308+
};
309+
{% endraw %}
310+
311+
A useful set of community-driven [collection](https://github.com/addyosmani/polymer-filters) of {{site.project_title}} filters is available if you need further
312+
examples of global filters.
313+
314+
#### Importing global filters
315+
316+
To use a filter across multiple elements, define it in a script and import it at the top of your element
317+
(after polymer.html).
318+
319+
320+
<link rel="import" href="uppercase-filter.html"/>

0 commit comments

Comments
 (0)