Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember switch/index if, etc

Tags:

ember.js

Ember seems to be quite lacking in the comparison department. You can only compare to zero using the #if helper and all the cool extended helpers people have written do not work with @index..., so you can't do something like

{{#each}}
    {{#xif '@index == 3'}}
        ....
    {{/xif}}
{{/each}}

which would provide specialization(for @index == 3).

Does anyone know how to essentially do the above? I have an loop but I need to specialize on certain indices.

ideally a switch statement would be nice(possible to hack handlebars to add this?)

e.g,

{{#each}}
    {{#switch @index}}
        {{case 0}}
            ....
        {{/case}}
        {{case 1}}
            ....
        {{/case}}
        {{default}}
            ....
        {{/default}}
    {{/switch}}
{{/each}}

etc...

Right now I'm having to use jquery to specialize the loop which makes for an inconsistent experience.

like image 357
user3606799 Avatar asked Jan 18 '26 10:01

user3606799


1 Answers

Up front, I agree, a comparison if would be awesome, but the powers that be are against it since you're putting "too much logic in the template."

Index can be accessed using _view.contentIndex

http://emberjs.jsbin.com/mugokaka/1/edit

And switch is just syntactic sugar for an if statement, so it can still be accomplished, but again, it isn't a comparison, just truthy, and a little ugly.

{{#if statementOne}}
   foo 1
{{else}}
  {{#if statementTwo}}
     foo 2
  {{else}}
    //yadda
  {{/if}}
{{/if}}

They generally recommend putting the logic in the controller, and having it return a truthy statement.

like image 65
Kingpin2k Avatar answered Jan 21 '26 00:01

Kingpin2k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!