biesior
biesior

Reputation: 55798

Ember/Handlebars: compare variable with string

I need to display some block depending on the current views variable in comparison to some string:

PSEUDO code is:

{{#if view.someVariable "desiredValue"}}
    Hurray desiredValue exists in this  view!
{{else}}
    Sorry, doesn't match...
{{/if}}

Of course it doesn't work as if statement allows only one param. I tried with custom registerHelper of Handlebars, but it doesn't resolve the view.someVariable, instead uses it as a string (although view.someVariable is defined and has value).

Finally I also tried with Handlebar's registerBoundHelper - it resolves the params BUT doesn't support Handlebar's blocks, what's more tries to resolve the string as an object, so fail again. Pure madness!

My views are very dynamical they depends on many factors also from backend and approach from the pseudo code would be perfect for resolving it. Is there any way to do that?

Edit: There is a sample of what I want ... http://jsfiddle.net/biesior/dMS2d/ looks quite trivial...

[if] statement inside the each is pseudo code of course

Upvotes: 1

Views: 5374

Answers (1)

Gosha A
Gosha A

Reputation: 4570

You can just declare a computed property, something like isDesired on your view, which would compare someVariable to your string:

App.MyView = Ember.View.extend({
  // ...stuff...
  isDesired: Ember.computed.equal('someVariable', 'desiredValue')
});

And have a simple conditional in the template:

{{#if view.isDesired}}
  Hurray desiredValue exists in this view!
{{else}}
  Sorry, doesn't match...
{{/if}}

Upvotes: 1

Related Questions