UNPKG

2.44 kBTypeScriptView Raw
1declare const _default: object;
2/**
3 The `fn` helper allows you to ensure a function that you are passing off
4 to another component, helper, or modifier has access to arguments that are
5 available in the template.
6
7 For example, if you have an `each` helper looping over a number of items, you
8 may need to pass a function that expects to receive the item as an argument
9 to a component invoked within the loop. Here's how you could use the `fn`
10 helper to pass both the function and its arguments together:
11
12 ```app/templates/components/items-listing.hbs
13 {{#each @items as |item|}}
14 <DisplayItem @item=item @select={{fn this.handleSelected item}} />
15 {{/each}}
16 ```
17
18 ```app/components/items-list.js
19 import Component from '@glimmer/component';
20 import { action } from '@ember/object';
21
22 export default class ItemsList extends Component {
23 handleSelected = (item) => {
24 // ...snip...
25 }
26 }
27 ```
28
29 In this case the `display-item` component will receive a normal function
30 that it can invoke. When it invokes the function, the `handleSelected`
31 function will receive the `item` and any arguments passed, thanks to the
32 `fn` helper.
33
34 Let's take look at what that means in a couple circumstances:
35
36 - When invoked as `this.args.select()` the `handleSelected` function will
37 receive the `item` from the loop as its first and only argument.
38 - When invoked as `this.args.select('foo')` the `handleSelected` function
39 will receive the `item` from the loop as its first argument and the
40 string `'foo'` as its second argument.
41
42 In the example above, we used an arrow function to ensure that
43 `handleSelected` is properly bound to the `items-list`, but let's explore what
44 happens if we left out the arrow function:
45
46 ```app/components/items-list.js
47 import Component from '@glimmer/component';
48
49 export default class ItemsList extends Component {
50 handleSelected(item) {
51 // ...snip...
52 }
53 }
54 ```
55
56 In this example, when `handleSelected` is invoked inside the `display-item`
57 component, it will **not** have access to the component instance. In other
58 words, it will have no `this` context, so please make sure your functions
59 are bound (via an arrow function or other means) before passing into `fn`!
60
61 See also [partial application](https://en.wikipedia.org/wiki/Partial_application).
62
63 @method fn
64 @public
65*/
66export default _default;
67//# sourceMappingURL=fn.d.ts.map
\No newline at end of file