How do we check for a value equality in ember.js's If-block helper?
{{#if person=="John"}}
How do we perform above in handlebars?
A Handlebars helper call is a simple identifier, followed by zero or more parameters (separated by a space). Each parameter is a Handlebars expression that is evaluated exactly the same way as described above in "Basic Usage": template {{firstname}} {{loud lastname}}
To call JavaScript function from Handlebars, we can register a helper. to add a template that uses the printItems helper. Then we create the printItems helper and use it by writing: Handlebars.
To iterate over an object with JavaScript and Handlebars. js, we can use the #each keyword. to add the Handlebars script and the template for looping through each entry in data . We use #each this to loop through each array property.
The {{#if}}
helper can only test for properties, not arbitrary expressions. The best thing to do in cases like this is therefore to write a property computing whatever conditional you want to test for.
personIsJohn: function() {
return this.get('person') === 'John';
}.property('person')
Then do {{#if personIsJohn}}
.
Note: If you find this too limiting, you can also register your own more powerful if
helper.
Use an Ember.Component
, thus avoid repetitively defining computed properties in your classes (like personIsJohn
above):
// if_equal_component.js script
App.IfEqualComponent = Ember.Component.extend({
isEqual: function() {
return this.get('param1') === this.get('param2');
}.property('param1', 'param2')
});
// if-equal.handlebars template
{{#if isEqual}}
{{yield}}
{{/if}}
You can define the else part of the comparison, with an App.ElseEqualComponent
:
// else_equal_component.js script
App.ElseEqualComponent = App.IfEqualComponent.extend();
// else-equal.handlebars template
{{#unless isEqual}}
{{yield}}
{{/unless}}
{{#if-equal param1=person param2="John"}}
Hi John!
{{/if-equal}}
{{#else-equal param1=person param2="John"}}
Who are you?
{{/else-equal}}
If you're using HTMLBars (Ember version 1.10+), then you can use the Ember Truth Helper addon:
https://github.com/jmurphyau/ember-truth-helpers
Once installed, it'll be as simple as this:
{{#if (eq person "John")}} hello {{/if}}
although this problem can be solved using eq helper by writing
{{#if (eq person "John")}} hello {{/if}}
but for a general solution you can make your own helper which will take three params param[0]
and param[2]
being operand and param[1]
being operator. Below is the helper file.
compare.js
import Ember from 'ember';
export function compare(params) {
if(params[3]){ //handle case insensitive conditions if 4 param is passed.
params[0]= params[0].toLowerCase();
params[2]= params[2].toLowerCase();
}
let v1 = params[0];
let operator = params[1];
let v2 = params[2];
switch (operator) {
case '==':
return (v1 == v2);
case '!=':
return (v1 != v2);
case '===':
return (v1 === v2);
case '<':
return (v1 < v2);
case '<=':
return (v1 <= v2);
case '>':
return (v1 > v2);
case '>=':
return (v1 >= v2);
case '&&':
return !!(v1 && v2);
case '||':
return !!(v1 || v2);
default:
return false;
}
}
export default Ember.Helper.helper(compare);
now you can easily use it for multiple purpose.
for equality check.
{{#if (compare person '===' 'John')}} {{/if}}
for greater check.
{{#if (compare money '>' 300)}} {{/if}}
and so on.
Expanding on Jo Liss's answer, you can now do this using a computed property macro for more concise and readable code.
personIsJohn: function() {
return this.get('person') === 'John';
}.property('person')
becomes
personIsJohn: Ember.computed.equal('person', 'John')
Relavent Docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With