Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb Meteor中对象的发布计数_Mongodb_Meteor_Meteor Publications - Fatal编程技术网

Mongodb Meteor中对象的发布计数

Mongodb Meteor中对象的发布计数,mongodb,meteor,meteor-publications,Mongodb,Meteor,Meteor Publications,在我的服务器端,我为各种对象提供了一个基本上返回计数的发布。每个不同的对象都有不同的发布名称,如下所示: Meteor.publish('object1Count', function(... Meteor.publish('object2Count', function(... Meteor.publish('object1Count', function(arg) { var self = this; var count = 0; var initializing = true; va

在我的服务器端,我为各种对象提供了一个基本上返回计数的发布。每个不同的对象都有不同的发布名称,如下所示:

Meteor.publish('object1Count', function(...

Meteor.publish('object2Count', function(...
Meteor.publish('object1Count', function(arg) {
var self = this;
var count = 0;
var initializing = true;

var query = arg?{arg:arg}:{};
var projection = !arg?{limit:1}:{};

var handle = Object1.find(query, projection).observeChanges({
  added: function (idx) {
    count++;
    if (!initializing)
      self.changed("totalcounts", 1, {count: count});  
  },
  removed: function (idx) {
    count--;
    self.changed("totalcounts", 1, {count: count});  
  }
});

initializing = false;

self.added("totalcounts", 1, {count: count});

self.ready();

self.onStop(function () {
  handle.stop();
});

}); 
template.subscribe('object1Count', template.reactiveEventId.get());
...
TotalCounts = (typeof TotalCounts==='undefined')?new Mongo.Collection("totalcounts"):TotalCounts;
是这样的:

Meteor.publish('object1Count', function(...

Meteor.publish('object2Count', function(...
Meteor.publish('object1Count', function(arg) {
var self = this;
var count = 0;
var initializing = true;

var query = arg?{arg:arg}:{};
var projection = !arg?{limit:1}:{};

var handle = Object1.find(query, projection).observeChanges({
  added: function (idx) {
    count++;
    if (!initializing)
      self.changed("totalcounts", 1, {count: count});  
  },
  removed: function (idx) {
    count--;
    self.changed("totalcounts", 1, {count: count});  
  }
});

initializing = false;

self.added("totalcounts", 1, {count: count});

self.ready();

self.onStop(function () {
  handle.stop();
});

}); 
template.subscribe('object1Count', template.reactiveEventId.get());
...
TotalCounts = (typeof TotalCounts==='undefined')?new Mongo.Collection("totalcounts"):TotalCounts;
但正如您在这些方法中看到的,每种方法中都会有这一行

self.added("totalcounts", 1, {count: count}); 
事实上,在客户端,当我需要访问对象的计数时,我会这样做:

Meteor.publish('object1Count', function(...

Meteor.publish('object2Count', function(...
Meteor.publish('object1Count', function(arg) {
var self = this;
var count = 0;
var initializing = true;

var query = arg?{arg:arg}:{};
var projection = !arg?{limit:1}:{};

var handle = Object1.find(query, projection).observeChanges({
  added: function (idx) {
    count++;
    if (!initializing)
      self.changed("totalcounts", 1, {count: count});  
  },
  removed: function (idx) {
    count--;
    self.changed("totalcounts", 1, {count: count});  
  }
});

initializing = false;

self.added("totalcounts", 1, {count: count});

self.ready();

self.onStop(function () {
  handle.stop();
});

}); 
template.subscribe('object1Count', template.reactiveEventId.get());
...
TotalCounts = (typeof TotalCounts==='undefined')?new Mongo.Collection("totalcounts"):TotalCounts;
它显然有效,但现在我读了两遍,我想知道为什么所有对象的“totalcounts”集合看起来都一样,所以如果我在需要不同totalcounts的页面之间切换(对于不同的对象),我猜客户端会破坏本地集合totalcounts并创建一个新的集合。服务器端也会发生这种情况吗

所以最后我的问题是:什么是最佳实践?由于各种原因,项目需要总计数:分页、图表等。。我想在服务器端创建总计数,并只传递该计数的最小数据。我应该为每个对象创建不同的“totalcounts”吗?这样做的有效方法是什么

谢谢

它意味着向集合名称添加
totalcounts
id为
\u的文档为
1
,其余数据为
{count:count}
。 因为它们具有相同的_id,所以您只能订阅一次。
顺便说一句,当模板“卸载”时,它将自动停止订阅。

Ohh我明白了,所以如果我尝试在同一模板中使用其中两个集合,我会发现有趣的数据(可能计数总是上次订阅的数据,我会尝试一下,看看它是否如预期的那样)。无论如何,创建不同的TotalCount(如object1totalcount、object2totalcount等)是否正确。。?有关于最佳实践的参考吗?我认为这取决于您的系统功能。但您应该尽可能少地使用订阅,并使用方法加载数据以获得更好的性能。