3gwebtrain
3gwebtrain

Reputation: 15293

How to interpret with each loop?

I am trying to interpret with each loop. but I am not able to come with a good solution( ember way! )

how to handle this scernario?

From the loop I am printing 16 digit. whenever the index reach 4 ( index % 4 == 0 ) I would like to add a hyphen.(-)

how to achive this?

here is my route.js :

import Ember from 'ember';

    export default Ember.Route.extend({
      model(){
        return [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
      }
    });

my hbs file:

<h1>Each with condition </h1>
{{#each model as |num index |}}
    {{index}}
 {{/each}}

but I look like:

<h1>Each with condition </h1>
{{#each model as |num index |}}
    {{index}} {{ if index % 4 == 0 }} --  {{/if}}
 {{/each}}

So, what is the correct ember approach for this?

Twiddle here

Upvotes: 0

Views: 34

Answers (1)

Ember Freak
Ember Freak

Reputation: 12872

You need to write your own helper to achieve this. I updated your twiddle

helpers/my-helper.js

import Ember from 'ember';
export function myHelper([index,position,totalLength]) {
  if(index === totalLength-1){
    return '';
  }
  index = index+1;
  return index%position === 0 ? " -- ":"";
}
export default Ember.Helper.helper(myHelper);

in application.hbs,

{{#each model as |num index |}}
    {{index}}{{my-helper index 4 model.length}}
 {{/each}}

Upvotes: 3

Related Questions