我需要告知客户有关服务器端的更改.在我的情况下,我在服务器和客户端上使用不同的集合(在这个问题中更多关于它:如何使用meteor.js构建像页面一样的pinterest).
在服务器上,我从外部API获取新产品.我想向所有客户发布新项目的数量,他们可以更新布局运行良好所需的局部变量.怎么做?
如果我可以发布/订阅除Meteor.Collection之外的其他类型的数据,那将是很好的.我找到了Meteor.deps,但据我所知,它仅适用于客户端.
为了完成你想要的,你确实需要另一个集合 - 在客户端上。在服务器上的发布功能中,从头开始构建文档,将当前的产品计数分配给属性。使用observe() 并设置、修改count何时向产品添加或删除文档。count在客户端订阅“记录集”。
// Server
Meteor.publish('count', function () {
// Build a document from scratch
var self = this;
var uuid = Meteor.uuid();
var count = Products.find().count();
// Assign initial Products count to document attribute
self.set('count', uuid, {count: count});
// Observe Products for additions and removals
var handle = Products.find().observe({
added: function (doc, idx) {
count++;
self.set('counts', uuid, {count: count});
self.flush();
},
removed: function (doc, idx) {
count--;
self.set('counts', uuid, {count: count});
self.flush();
}
});
self.complete();
self.flush();
self.onStop(function () {
handle.stop();
});
});
// Client
Counts = new Meteor.Collection('count');
Meteor.subscribe('count');
console.log('Count: ' + Counts.findOne().count);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2674 次 |
| 最近记录: |