-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathwith.js
47 lines (39 loc) · 1.46 KB
/
with.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
@module ember
@submodule ember-templates
*/
import shouldDisplay from 'ember-views/streams/should_display';
/**
Use the `{{with}}` helper when you want to alias a property to a new name. This is helpful
for semantic clarity as it allows you to retain default scope or to reference a property from another
`{{with}}` block.
If the aliased property is "falsey", for example: `false`, `undefined` `null`, `""`, `0` or
an empty array, the block will not be rendered.
```handlebars
{{! Will only render if user.posts contains items}}
{{#with user.posts as |blogPosts|}}
<div class="notice">
There are {{blogPosts.length}} blog posts written by {{user.name}}.
</div>
{{#each blogPosts as |post|}}
<li>{{post.title}}</li>
{{/each}}
{{/with}}
```
Without the `as` operator, it would be impossible to reference `user.name` in the example above.
NOTE: The alias should not reuse a name from the bound property path.
For example: `{{#with foo.bar as |foo|}}` is not supported because it attempts to alias using
the first part of the property path, `foo`. Instead, use `{{#with foo.bar as |baz|}}`.
@method with
@for Ember.Templates.helpers
@param {Object} options
@return {String} HTML string
@public
*/
export default function withHelper(params, hash, options) {
if (shouldDisplay(params[0])) {
options.template.yield([params[0]]);
} else if (options.inverse && options.inverse.yield) {
options.inverse.yield([]);
}
}