Meteor:如何使更新 Meteor.user 字段具有响应性

Meteor: How do i make Updating Meteor.user fields reactive?

本文关键字:Meteor 响应 字段 user 何使 更新      更新时间:2023-09-26

我有一组称为"类别"的对象,其中有属于该类别的帖子。用户可以通过单击获取该类别对象中的数据的按钮来"关注"类别,最重要的是,帖子将它们放在Meteor.users中的一个字段中。就目前而言,用户仅获得用户单击按钮时可用的帖子。

我该如何做到这一点,以便当他们"关注"一个类别时,在点击事件已经完成后,稍后出现的任何新帖子都将自动推送到他们的用户数据。换句话说,如何使这个过程是被动的?

客户/类别.js

Template.CategoriesMain.events({
  'click .toggle-category': function(e){
          var ob = $(e.target).parent().find("div").text();
          var id = $.makeArray( ob );
          console.log(id);
          e.preventDefault();
          Meteor.call('addingCategory', id, function(error, user){ console.log(id)});
      }
});

服务器/类别.js

Meteor.publish("userData", function () {
  if (this.userId) {
    return Meteor.users.find({_id: this.userId},
                             {fields: {'name': 1}});
  } else {
    this.ready();
  }
});

我可以用自动运行做点什么吗?

Template.CategoriesMain.events({
  'click .toggle-category': function(e){
    autorun(function() { 
          var ob = $(e.target).parent().find("div").text();
          var id = $.makeArray( ob );
          console.log(id);
          e.preventDefault();
          Meteor.call('addingCategory', id, function(error, user){ console.log(id)});
      }
  });    
});

目前无法获取它,但请尝试

Meteor.publish("userData", function () {  if (this.userId) {    返回 Meteor.users.find({_id: this.userId},                             {字段: {'名称': 1}});  } else {    返回空值;  }  this.ready();});