Skip to content

Commit d02f263

Browse files
committed
Adds global filters docs for Polymer#571
1 parent 6de6398 commit d02f263

File tree

1 file changed

+74
-13
lines changed

1 file changed

+74
-13
lines changed

docs/polymer/expressions.md

Lines changed: 74 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,64 @@ 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 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

Comments
 (0)