javascript - Meteor helper called multiple times by single template variable - Stack Overflow

Tweets = new Meteor.Collection('tweets');if (Meteor.isClient) {Meteor.subscribe('tweets&

Tweets = new Meteor.Collection('tweets');

if (Meteor.isClient) {

  Meteor.subscribe('tweets');

  Template.Panel.helpers({
    items: function() {
      var days_tweets = Tweets.find();
      console.log(days_tweets.count());
      return days_tweets;
    });
  }

if (Meteor.isServer) {
  Meteor.publish('tweets', function() {
    return Tweets.find({}, {limit: 1000});
  });

The template:

<body>
<h1>This is a list of tweets</h1>
  {{> Panel}}
</body>

<template name="Panel">
<h2>A list of tweets sorted by size</h2>
    {{#each items}}
        <p>item</p>
    {{/each}}
</template>

And the console output when the page loads:

Tweet count:  0
Tweet count:  129
Tweet count:  272
Tweet count:  366
Tweet count:  457
Tweet count:  547
Tweet count:  672
Tweet count:  814
Tweet count:  941
Tweet count:  1000

So the helper function fires 10 times on page load (the number of times varies). Could anyone explain what is happening here? I can't find any reference to this, accept in situations where the helper is called from multiple {{ }} on the template. Also any way to stop it? Eventually I need to process the tweets in one go before before they are rendered.

Tweets = new Meteor.Collection('tweets');

if (Meteor.isClient) {

  Meteor.subscribe('tweets');

  Template.Panel.helpers({
    items: function() {
      var days_tweets = Tweets.find();
      console.log(days_tweets.count());
      return days_tweets;
    });
  }

if (Meteor.isServer) {
  Meteor.publish('tweets', function() {
    return Tweets.find({}, {limit: 1000});
  });

The template:

<body>
<h1>This is a list of tweets</h1>
  {{> Panel}}
</body>

<template name="Panel">
<h2>A list of tweets sorted by size</h2>
    {{#each items}}
        <p>item</p>
    {{/each}}
</template>

And the console output when the page loads:

Tweet count:  0
Tweet count:  129
Tweet count:  272
Tweet count:  366
Tweet count:  457
Tweet count:  547
Tweet count:  672
Tweet count:  814
Tweet count:  941
Tweet count:  1000

So the helper function fires 10 times on page load (the number of times varies). Could anyone explain what is happening here? I can't find any reference to this, accept in situations where the helper is called from multiple {{ }} on the template. Also any way to stop it? Eventually I need to process the tweets in one go before before they are rendered.

Share asked Nov 13, 2014 at 16:05 kendletekendlete 2244 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

When you do a find meteor registers a dependency for that template helper on the collection you did a find on. Because of that dependency meteor will call the template helper for every modification to the collection.

If you haven't subscribed yet there is no data loaded in the client side copy of your mongo collection. Only when you call subscribe will meteor start pulling in the data from the server.

So the method is called multiple times because the subscribe keeps inserting new documents into your local copy of the mongo collection, triggering new calls to the template helper.

Best pattern to counter any problems this may give is by subscribing in the helper and using the ready method on the subscribe documentation. Ready is also reactive so when all data is pulled in ready will be changed to true and the helper will be called again.

  Template.Panel.helpers({
      items: function() {
          var ready = Meteor.subscribe('tweets').ready();
          var days_tweets = Tweets.find();

          return {
              data: days_tweets,
              ready: ready
          };
      });
  }

Template itself:

{{#with items}}
     {{#if ready}}
         {{#each data}}
             <p>item</p>
         {{/each}}
     {{else}}
         Show a spinner or whatever
     {{/if}}
{{/with}}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744917803a4600943.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信