Jon*_*and 11 javascript meteor iron-router
是否可以只向用户发布集合的计数?我想在主页上显示总计数,但不会将所有数据传递给用户.这是我尝试但它不起作用:
Meteor.publish('task-count', function () {
return Tasks.find().count();
});
this.route('home', {
path: '/',
waitOn: function () {
return Meteor.subscribe('task-count');
}
});
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我得到一个无尽的加载动画.
sai*_*unt 16
Meteor.publish函数应该返回游标,但是这里你直接返回一个Number,即Tasks集合中文档的总数.
如果你想以正确的方式做到这一点,在Meteor中计算文件是一项令人惊讶的难度任务:使用既优雅又有效的解决方案.
包ros:publish-counts(tmeasday的分支:publish-counts)使用该fastCount选项为小型集合(100-1000)或更大集合(数万)的"近乎准确"计数提供准确计数.
你可以这样使用它:
// server-side publish (small collection)
Meteor.publish("tasks-count",function(){
Counts.publish(this,"tasks-count",Tasks.find());
});
// server-side publish (large collection)
Meteor.publish("tasks-count",function(){
Counts.publish(this,"tasks-count",Tasks.find(), {fastCount: true});
});
// client-side use
Template.myTemplate.helpers({
tasksCount:function(){
return Counts.get("tasks-count");
}
});
Run Code Online (Sandbox Code Playgroud)
您将获得客户端响应计数以及服务器端合理的性能实现.
这个问题在(付费)防弹Meteor课程中讨论,这是一个推荐阅读:https://bulletproofmeteor.com/
我会用一个 Meteor.call
客户:
var count; /// Global Client Variable
Meteor.startup(function () {
Meteor.call("count", function (error, result) {
count = result;
})
});
Run Code Online (Sandbox Code Playgroud)
回来count帮忙
服务器:
Meteor.methods({
count: function () {
return Tasks.find().count();
}
})
Run Code Online (Sandbox Code Playgroud)
*注意这个解决方案不会被动.但是,如果需要反应性,则可以加入.
| 归档时间: |
|
| 查看次数: |
3943 次 |
| 最近记录: |