[英]How to show the total count of items and a limit list of items on a same template
I have two sections on my page. 我的页面上有两个部分。
The first section has a limited list of items. 第一部分的项目列表有限。 The Second section has a total count of items (recordsCount).
第二部分具有项目总数(recordsCount)。
When a server adds a new item I see the list of items is updated but the total count has an old value. 服务器添加新项目时,我看到项目列表已更新,但总计数具有旧值。
Tracks = new Mongo.Collection('tracks')
Client: 客户:
Meteor.subscribe('tracks')
Meteor.call('records', function(err, data) {
Session.set('_records', data)
})
Template.tracks.helpers({
tracks: function() {
return Tracks.find()
},
recordsCount: function() {
return Session.get('_records')
}
})
Server: 服务器:
Meteor.publish('tracks', function() {
return Tracks.find({}, {limit: 100})
})
Meteor.methods({
records: function() {
return Tracks.find({}).count()
}
})
var CronManager = new Cron(10000)
CronManager.addJob(1, function() {
Tracks.insert({filed1: 'test'})
})
If you just want to efficiently bring counts into your app, check out the publish-counts package. 如果您只是想有效地将计数引入您的应用程序,请查看publish-counts软件包。
Meteor.publish('posts', function(author) {
var cursor = Tracks.find(...);
Counts.publish(this, 'tracks-count', cursor, {nonReactive: true});
});
The nonReactive
options sends the count only on demand. nonReactive
选项仅按需发送计数。 This can be useful if you don't really need real-time counts, since most apps can do fine with updates every few seconds. 如果您实际上不需要实时计数,这将很有用,因为大多数应用程序每隔几秒钟就可以完成更新。 That will save a lot of CPU.
这样可以节省大量CPU。
Template.tracks.helpers({
tracks: function() {
return Counts.get('posts-count')
}
});
Hattip to Arunoda's excellent chapter on this from Bulletproof Meteor, Counting Documents . Hattip从《 防弹流星,计数文件》中获得了Arunoda出色的一章。
I've investigated the solution with the publish-counts package. 我已经使用publish-counts包研究了该解决方案。 In my case it is the very heavy package.
就我而言,这是非常沉重的包裹。 It was loading CPU all the time.
它一直在加载CPU。 I've replace my server side code to use a collection with count field.
我已经替换了服务器端代码,以使用带有count字段的集合。
Common: 共同:
Counter = new Mongo.Collection('count')
Server: Meteor.Publish('count', function() { return Counter.find() }) 服务器:Meteor.Publish('count',function(){return Counter.find()})
if(Counter.find().count() === 0) Counter.insert({count: Tracks.find().count()})
var CronManager = new Cron(10000)
CronManager.addJob(1, function() {
Counter.update({}, {$inc: {count: 1}})
Tracks.insert({filed1: 'test'})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.