简体   繁体   中英

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

I have a set of objects called 'categories' which have posts in them that belong to that category. A user is able to 'follow' a category by clicking a button that takes the data within that category object, most importantly the posts puts them in a field within Meteor.users. As it stands, the user only gets the posts that were available at the when the user clicked the button.

How do i make it so that when they 'follow' a category, any new posts that come in later, after the click event has already been done, will automatically be pushed to their user data. In other words, how do make this process reactive?

client/categories.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)});
      }
});

server/categories.js

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

could i do something with autorun? to the tune of this?

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)});
      }

  });    
});

can't get it currently but try

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

  this.ready();

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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