编辑:我使用的解决方案是@Kyll 的解决方案。
假设我想返回的服务器端对象“复杂”构建并且需要来自不同集合的不同属性。
我第一次尝试:
/服务器/publications.js
Meteor.publish('myCustomDocument', function(){
// suppose here that I need to X.find() different collections
// and create a complex Array of JSON data (which contains different
// attributes from different Collections
return [
{appName: 'aName',
category: 'catName',
anotherField: 'something'},
(...)
];
});
它不起作用,因为它没有返回游标。我想要做的是创建一个由不同集合构建的文档(或一组文档)。
我不需要观察该文件的更改。
我为它创建了一个集合:
/collections/myCollection.js
MyCollection = new Meteor.Collection('myCollection');
在客户端,使用 铁路由器 ,我试图做的是:
/lib/router.js
this.route('myPage',{
path: '/myPage',
waitOn: function(){ return Meteor.subscribe('myCollection'); },
data: function(){ return MyCollection.find(); }
});
我将如何实现向客户端发送非 react 性数据?
最佳答案
如果数据不会经常更改,则使用方法可能更有意义。这里也可以使用发布/订阅模式,但不是返回游标或任何东西,您需要手动使用发布“齿轮”,如下所示:
Meteor.publish("myCustomPublication", function () {
// here comes some custom logic
this.added("myCollection", someUniqueId, someCustomData);
this.ready(); // without this waitOn will not work
});
关于 meteor : how to publish custom JSON data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28812306/