-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12920 from GavinJoyce/gj/glimmer-inline-if
[Glimmer2] inline if
- Loading branch information
Showing
8 changed files
with
243 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
@module ember | ||
@submodule ember-templates | ||
*/ | ||
|
||
import { toBool as emberToBool } from './if-unless'; | ||
import { assert } from 'ember-metal/debug'; | ||
|
||
/** | ||
The inline `if` helper conditionally renders a single property or string. | ||
This helper acts like a ternary operator. If the first property is truthy, | ||
the second argument will be displayed, if not, the third argument will be | ||
displayed | ||
```handlebars | ||
{{if useLongGreeting "Hello" "Hi"}} Alex | ||
``` | ||
You can use the `if` helper inside another helper as a subexpression. | ||
```handlebars | ||
{{some-component height=(if isBig "100" "10")}} | ||
``` | ||
@method if | ||
@for Ember.Templates.helpers | ||
@public | ||
*/ | ||
export default function inlineIf(args) { | ||
assert( | ||
'The inline form of the `if` and `unless` helpers expect two or ' + | ||
'three arguments, e.g. `{{if trialExpired \'Expired\' expiryDate}}` ', | ||
args.length === 2 || args.length === 3 | ||
); | ||
|
||
if (emberToBool(args[0])) { | ||
return args[1]; | ||
} else { | ||
//TODO: always return `args[2]` post glimmer2: https://github.com/emberjs/ember.js/pull/12920#discussion_r53213383 | ||
let falsyArgument = args[2]; | ||
return falsyArgument === undefined ? '' : falsyArgument; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
packages/ember-glimmer/tests/integration/helpers/inline-if-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { moduleFor } from '../../utils/test-case'; | ||
import { set } from 'ember-metal/property_set'; | ||
|
||
import { | ||
BASIC_TRUTHY_TESTS, | ||
BASIC_FALSY_TESTS, | ||
SharedHelperConditionalsTest | ||
} from '../../utils/shared-conditional-tests'; | ||
|
||
moduleFor('Helpers test: inline {{if}}', class extends SharedHelperConditionalsTest { | ||
|
||
templateFor({ cond, truthy, falsy }) { | ||
return `{{if ${cond} ${truthy} ${falsy}}}`; | ||
} | ||
|
||
['@test it can omit the falsy argument']() { | ||
this.render(`{{if cond1 'T1'}}{{if cond2 'T2'}}`, { cond1: true, cond2: false }); | ||
|
||
this.assertText('T1'); | ||
|
||
this.runTask(() => this.rerender()); | ||
|
||
this.assertText('T1'); | ||
|
||
this.runTask(() => set(this.context, 'cond1', false)); | ||
|
||
this.assertText(''); | ||
|
||
this.runTask(() => { | ||
set(this.context, 'cond1', true); | ||
set(this.context, 'cond2', true); | ||
}); | ||
|
||
this.assertText('T1T2'); | ||
|
||
this.runTask(() => { | ||
set(this.context, 'cond1', true); | ||
set(this.context, 'cond2', false); | ||
}); | ||
|
||
this.assertText('T1'); | ||
} | ||
|
||
['@test it raises when there are more than three arguments']() { | ||
expectAssertion(() => { | ||
this.render(`{{if condition 'a' 'b' 'c'}}`, { condition: true }); | ||
}, /The inline form of the `if` and `unless` helpers expect two or three arguments/); | ||
} | ||
|
||
['@test it raises when there are less than two arguments']() { | ||
expectAssertion(() => { | ||
this.render(`{{if condition}}`, { condition: true }); | ||
}, /The inline form of the `if` and `unless` helpers expect two or three arguments/); | ||
} | ||
|
||
}, BASIC_TRUTHY_TESTS, BASIC_FALSY_TESTS); | ||
|
||
moduleFor('@glimmer Helpers test: {{if}} used with another helper', class extends SharedHelperConditionalsTest { | ||
|
||
wrapperFor(templates) { | ||
return `{{concat ${templates.join(' ')}}}`; | ||
} | ||
|
||
templateFor({ cond, truthy, falsy }) { | ||
return `(if ${cond} ${truthy} ${falsy})`; | ||
} | ||
|
||
}); | ||
|
||
moduleFor('@glimmer Helpers test: {{if}} used in attribute position', class extends SharedHelperConditionalsTest { | ||
|
||
wrapperFor(templates) { | ||
return `<div data-foo="${templates.join('')}" />`; | ||
} | ||
|
||
templateFor({ cond, truthy, falsy }) { | ||
return `{{if ${cond} ${truthy} ${falsy}}}`; | ||
} | ||
|
||
textValue() { | ||
return this.$('div', this.element).attr('data-foo'); | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.