简体   繁体   English

模板助手被多次调用

[英]Template helper gets called multiple times

My intention is to retrieve one random entry from a collection and display it on the website - if all sentences are through (read: the user has "seen" them), display something else (therefore a dummy sentence gets returned). 我的意图是从集合中检索一个随机条目并将其显示在网站上-如果所有句子都通过了(阅读:用户“看到”了它们),则显示其他内容(因此返回了一个虚拟句子)。 But, on server start and on button-click events, this helper gets fired at least twice. 但是,在服务器启动和按钮单击事件上,此助手至少被触发两次。 Here is some code: 这是一些代码:

In client.js: 在client.js中:

Template.registerHelper('random_sentence', function() {
  fetched = _.shuffle(Sentences.find({
    users: {
      $nin: [this.userId]
    }
  }).fetch())[0];
  if (fetched === undefined) {
    return {
      sentence: "done",
      _id: 0,
      done: true
    };
  }
  Session.set('question', fetched._id);
  console.log(fetched);
  return fetched;
  });

The helper function for the template: 模板的辅助功能:

sent: function(){
  sent = Session.get('question');
  return Sentences.findOne(sent);
}

in main template: 在主模板中:

{{#with random_sentence}}
  {{#if done}}
    <!-- Display something else -->
  {{else}}
    <div class="container">
      {{> question}}
    </div>
  {{/if}}
{{/with}}

the "question" template: “问题”模板:

<div class="well">
  <div class="panel-body text-center">
    <h3>{{sent.sentence}}</h3>
  </div>
</div>

If I don't return anything in the "random_sentences"-function,nothing get's displayed. 如果我在“ random_sentences”功能中未返回任何内容,则不会显示任何内容。

I don't know where my "logic failure" is situated? 我不知道我的“逻辑故障”在哪里? I'm new to meteor - so I might overlook something obvious. 我是新来的流星-所以我可能会忽略一些显而易见的事情。

Thanks in advance :-) 提前致谢 :-)

UPDATE: This is how I intended to get the new sentence and display it: 更新:这就是我打算获取新句子并显示它的方式:

Template.answer.events({
  'click': function(event) {
    var text = event.target.getAttribute('id');
    if (text !== null) {
      var question = Session.get('question');
      var setModifier = {
        $inc: {}
      };
    setModifier.$inc[text] = 1;
    Sentences.update(question, setModifier);
    Meteor.call('update_user', question);
    Notifications.success('Danke!', 'Deine Beurteilung wurde gespeichert.');
    Blaze.render(Template.question, document.head);
    }
  }
});

In server.js (updating the question and a counter on the user): 在server.js中(更新问题和用户的计数器):

Meteor.methods({
  update_user: function(question) {
    Sentences.update(question, {
      $push: {
        "users": this.userId
      }
    });
    Meteor.users.update({
      _id: this.userId
    }, {
      $inc: {
        "profile.counter": 1
      }
    });
  },
});

I found the Blaze.render function somewhere on the web. 我在网络上的某个地方找到了Blaze.render函数。 the "document.head" part is simply because this function needs a DOM Element to render to, and since document.body just "multiplies" the body, I ust moved it to the head. “ document.head”部分仅是因为此函数需要一个DOM元素呈现,并且因为document.body只是“复制”了主体,所以我必须将其移到头部。 (DOM logic isn't my strong part). (DOM逻辑不是我的强项)。

An Idea I had: would it make the whole idea simpler to implement with iron-router? 我有一个主意:使用铁路由器可以使整个主意更容易实现吗? atm. 大气压。 I wanted to create a "one-page app" - I therefore thought that I don't need a router there. 我想创建一个“一页应用程序”-因此,我认为那里不需要路由器。

Another problem: Getting this logic to work (User gets one random sentence, which he has not seen) and publishing small sets of the collection (so the Client don't have to download 5 MB of data before using). 另一个问题:使这种逻辑起作用(用户得到了一个随机句子,这是他从未见过的),并发布了少量的集合(因此,客户端在使用之前不必下载5 MB的数据)。

Template helpers can be called multiple times so it's good to avoid making them stateful. 可以多次调用模板助手,因此最好避免使它们成为有状态的。 You're better off selecting the random entry in an onCreated or onRendered template handler. 最好在onCreatedonRendered 模板处理程序中选择随机条目 There you can do your random select, update the state, and put your choice in a Session variable to be retrieved by the helper. 在那里,您可以进行随机选择,更新状态,并将您的选择放入Session变量中,以供助手检索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM