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?
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?
Use an
Ember.Component
, thus avoid repetitively defining computed properties in your classes (likepersonIsJohn
above):You can define the else part of the comparison, with an
App.ElseEqualComponent
:Usage:
Expanding on Jo Liss's answer, you can now do this using a computed property macro for more concise and readable code.
becomes
Relavent Docs.
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:
although this problem can be solved using eq helper by writing
but for a general solution you can make your own helper which will take three params
param[0]
andparam[2]
being operand andparam[1]
being operator. Below is the helper file.compare.js
now you can easily use it for multiple purpose.
for equality check.
for greater check.
and so on.
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.Then do
{{#if personIsJohn}}
.Note: If you find this too limiting, you can also register your own more powerful
if
helper.