流星模板重新加载无穷大 [英] Meteor template reload infinity

查看:104
本文介绍了流星模板重新加载无穷大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Meteor运行时遇到问题.

I have a problem when running with Meteor.

我有一个问题"页面,我想在每次渲染计数视图时增加它.

I have a "question" page which I want to increase the count view whenever it is rendered.

所以我在模板函数中写

Template.questionview.helpers({
    question : function() {
      if(Session.equals('main_template_name', 'question')) {
        console.log(Session.get('question_id'));
        Questions.update({
          _id: Session.get('question_id')
        }, {
           $inc: {
           views: 1
        }
     });
   }
});

现在出现了问题,当我渲染问题视图并更新问题项时,该视图再次刷新,因为它是反射页面.然后是无限循环.

Now here comes the problem, when I render the question view and update the question item, the view is refreshed again because it is a reflective page. And then it comes infinity loop.

有人有建议吗?

推荐答案

通常,在这种情况下,模型存在问题.在这种情况下,我相信这是计数视图"的想法.有很多方法可以正确执行此操作.不需要在渲染上增加它,因为您正在UI代码中进行模型工作(在概念上和在实现上都已中断).

Typically, in situations like this, there is something broken about the model. In this case, I believe it's the "count view" idea. There are lots of ways to do this correctly. Incrementing it on render is not, since you're doing model work in UI code (broken conceptually and in implementation).

首先,将用户访问过的问题存储在某个地方.为什么不为用户提供{questionsVisited:[]}属性?

First, store the questions the user has visited somewhere. Why not a {questionsVisited:[]} property on the user?

使用Meteor.call(...)方法调用来注册视图:

Use a Meteor.call(...) method call to register a view instead:

Meteor.methods({
  viewQuestion: function(questionId) {
    // check if the user hasn't visited this question already
    var user = Meteor.users.findOne({_id:this.userId,questionsVisited:{$ne:questionId}});

    if (!user)
         return false;

    // otherwise, increment the question view count and add the question to the user's visited page
    Meteor.users.update({_id:this.userId},{$addToSet:{questionsVisited:questionId}});
    Questions.update({_id:questionId},{$inc:{views:1}});
    return true;
});

那么如何增加UI更改视图呢?好吧,让我们不做特别的事情.让我们在问题更改时仅 增加视图计数.

So how about incrementing the view on UI changes? Well, let's not do that specifically. Let's increment the view count only when the question changes.

Meteor.autorun(function () {
  var questionId = Session.get("question_id");
  Meteor.call('viewQuestion',questionId,function(e,r) {
    if (r)
      console.log("Question " + questionId + " logged an increment.");
    else 
      console.log("Question " + questionId + " has already been visited by user " + Meteor.userId();
  });
});

摆脱所有这些问题助手的东西...

And get rid of all this question helper stuff...

这甚至比您最初想要的要好.现在,同一用户的观看次数不再计算在内.如果这是所需的行为,请删除questionsVisited逻辑.

This is even better than what you originally wanted. Now views aren't counted twice for the same user. If that is the desired behavior, remove the questionsVisited logic.

在实际更改用户正在处理的逻辑问题时更改'question_id'会话变量.

Only change the 'question_id' session variable when you actually change the logical question the user is working with.

这篇关于流星模板重新加载无穷大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆