0

I need to know about to increment helper variable count in meteor.

For example :

    <head>
  <title>hello</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>

  {{> hello}}
</body>

<template name="hello">

  <button>Click Me</button>

  {{#each arr}}
     {{counter}} <!-- How to get the foo index here? --> {{name}}
  {{/each}}
</template>

Js Code :

 if (Meteor.isClient) {
  // counter starts at 0


  Template.hello.helpers({
    counter: function () {
      return Session.get('counter');
    }
  });

  Template.hello.helpers({
    arr: function () {
      console.log(Session.get('arrres'));
      return Session.get('arrres');
    }
  });

  Template.hello.events({
    'click button': function () {
      Session.set('counter', 0);

      Meteor.call('arrfun',10, function (error, res) {
        if (!error)
        { arrres = res;
          Session.set('arrres', arrres);
          console.log(res);
        }
        else{}
      } );

    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup

    Meteor.methods({

      arrfun:function arrfun(properties, callback)

      {
        var obj = [];
        for(var i = 0 ; i < 10; i++)
        {
          var obj1 = new Object();
          obj1.name = 'abc'+i;
          obj.push(obj1);
        }
        return obj;
      }
    });
  });
}

The above 'arr' contains list of names that present in object.Now can iterate 'arr' it will comes names.

now can we print names as like 1 abc 2 xyz until 'arr' completed.

So how can we print numbers from 1 to 'arr' length before names.

So please suggest me what to do for this.

user2344293
  • 173
  • 1
  • 13
  • Woah. `arr` has to be a number? Why not `count` or `total`? – Kyll Mar 27 '15 at 10:11
  • arr not a number for assume arr returns objs so objs length assume 10.then how to iterate 1 - 10 numbers in this loop.@Kyll – user2344293 Mar 27 '15 at 10:15
  • Could you please provide a pure HTML example of what you want to achieve? What the final result would look like in the browser. – Kyll Mar 27 '15 at 11:16
  • @user2344293 So you why can't you just use an
    because this will have numbering for you. No need for tricky over-engineering.
    – Ian Jones Mar 27 '15 at 11:36
  • possible duplicate of [How to get the index in meteor in each](http://stackoverflow.com/questions/26758925/how-to-get-the-index-in-meteor-in-each) – user3374348 Mar 27 '15 at 15:08
  • See: http://stackoverflow.com/questions/26758925/how-to-get-the-index-in-meteor-in-each Or if possible, just use `
      ` or some other CSS solution like Ian Jones suggests.
    – user3374348 Mar 27 '15 at 15:11

1 Answers1

0

It's a little hard to interpret your question but I think you want this:

Template.hello.events({
    'click button': function(event) {
        Session.set('counter', Math.round(Math.random() * 10));
    }
});

Template.hello.helpers({
    'arr': function() {
        var counter = Session.get('counter');
        var arr = [];
        for(var i = 0; i < counter; i++) {
            arr.push(i + 1);
        }
        return arr;
    }
});

And in your hello template:

<ul>
    {{#each arr}}
        <li>{{this}}</li>
    {{/each}}
</ul>

This will generate a random number between 0-10 every time you click the button, and then the template will count up to this random number in sequence.

Ian Jones
  • 1,369
  • 1
  • 10
  • 15
  • Yeah but it seems like overkill. I haven't found anything to simply repeat `n` times a block... Couldn't there be any other solution than to create a dummy array and iterate over it? – Kyll Mar 27 '15 at 10:26
  • Spacebars does not have a 'repeat loop n times' function. It solely relies on being passed either a cursor of objects or an array to iterate over. Hence the need to generate that array with the required number of elements. – Ian Jones Mar 27 '15 at 10:28
  • Dang that's lame. What about creating a custom block helper then? Something that could be defined once and easily be reused anywhere. – Kyll Mar 27 '15 at 10:30
  • That's not works out for me because i am return objs as 'arr' so how i can put these calculation in arr.that's why i am using counter helper even if we use ol it cames after no also . symbole .that .symbol also not need.@IanJones – user2344293 Mar 27 '15 at 10:40
  • @Kyll perhaps UI.registerHelper might actually be useful as mentioned here: https://www.discovermeteor.com/blog/spacebars-secrets-exploring-meteor-new-templating-engine/ – Ian Jones Mar 27 '15 at 10:45
  • @user2344293 I'm really having trouble understanding your english. Can you please edit your question to include an example output in pure HTML – Ian Jones Mar 27 '15 at 10:48
  • @Kyll: this is really an unrelated question, but here's a block helper you can use: http://meteorpad.com/pad/E4r2hu6jqhpEzwpAq/Counter – user3374348 Mar 27 '15 at 15:26