当发布中的集合子字段更改时订阅未更新(使用 $slice) [英] Subscription not updated when collection subfield changes in publication (using $slice)

查看:28
本文介绍了当发布中的集合子字段更改时订阅未更新(使用 $slice)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发布,它从订阅接收一个参数,以确定从集合内的数组返回多少条记录(使用 $slice).我遇到的问题是,当通过订阅发送新参数时,我的模板上的数据没有刷新.这是我的代码:

I have a publication which receives a parameter from a subscription to determine how many records to return from an array inside a collection (Using $slice). The issue I have is that my data is not refreshed on my template when a new parameter is sent through the subscription. This is my code:

出版物:

Meteor.publish('Conversations', function (record_limit) {
  var bunchsize = 50;
  var multiplier = 1;
  if (record_limit) {
    multiplier = record_limit;
  } 
  var publimit = bunchsize * multiplier;
  return Conversations.find({users:{$in:[this.userId]}},{fields:{"lstmsg":1,  "msg":{"$slice":-publimit}}});        
});

触发加载更多数组对象:

Trigger to load more array objects:

var convCounter = 1;
Template.conversation.events({
  'click .js-loadmore': function() {
    convCounter = convCounter + 1;
    Deps.autorun(function() { Meteor.subscribe("Conversations",convCounter); });
  }
});

有什么建议为什么我的模板上没有刷新订阅?它始终仅显示msg"字段的 50 个项目,而其上有 200 多个项目.提前致谢.

Any advice why the subscription is not being refreshed on my template? It always shows only 50 items for "msg" field while it has more than 200 items on it. Thanks in advance.

推荐答案

如文档中所述,当子字段级别(例如在我的情况下,在 msg 中)发生更改时,Meteor 似乎不会刷新订阅:

It seems that Meteor doesn't refresh subscriptions when a change happens in a subfield level (Such as in my case, inside msg) as is stated in the documentation:

目前,当多个订阅只发布同一个文档时在合并期间比较顶级字段.这意味着如果文档包含同一顶级的不同子字段字段,并非所有这些都将在客户端上可用.我们希望在未来的版本中取消此限制.

Currently, when multiple subscriptions publish the same document only the top level fields are compared during the merge. This means that if the documents include different sub-fields of the same top level field, not all of them will be available on the client. We hope to lift this restriction in a future release.

检查问题线程:https://github.com/meteor/meteor/issues/3764#issuecomment-75853901

问题仍未解决...大转弯.

Issue is still open... big turn around.

变通方法:我发现让它起作用的方法是每次我需要通过 $slice 参数从数组中检索更多字段时停止之前的订阅,如下所示:

WORKAROUND: The way I found to make it works was to stop the previous subscription each time I need to retrieve more fields from array through the $slice parameter, like this:

lastSub = "";
convCounter = 1;
Template.conversation.onCreated(function() {
   lastSub = Meteor.subscribe("Conversation", convCounter);
});

Template.conversation.events({
  'click .js-loadmore': function() {
     convCounter = convCounter + 1; 
     if (lastSub) {lastSub.stop();}
        lastSub = Meteor.subscribe("Conversation", convCounter);
  }   
});

该变通方法可能对数据访问效率低下,但我希望我的用户不会经常浏览旧对话.

The workaround could be inefficient for data access but I expect my users won't surf old conversations so often.

这篇关于当发布中的集合子字段更改时订阅未更新(使用 $slice)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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