Meteor:如何发布自定义 JSON 数据? [英] Meteor : how to publish custom JSON data?
问题描述
我使用的解决方案是@Kyll 的.
假设我想要返回的服务器端对象构建起来很复杂"并且需要来自不同集合的不同属性.
我第一次尝试:
/server/publications.js
I first tried:
/server/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'},
(...)
];
});
它不起作用,因为它没有返回游标.我想要做的是创建一个由不同集合构建的文档(或一组文档).
我不需要观察那个文件的变化.
It doesn't work because it's not returning a cursor. What I want to do is to create a document (or an array of documents) which is built from different collections.
I do not need to observe the changes on that document.
我为它创建了一个集合:
I have created a collection for it :
/collections/myCollection.js
/collections/myCollection.js
MyCollection = new Meteor.Collection('myCollection');
在客户端,使用iron-router,我尝试做的是:
On the client side, using iron-router, what I tried to do is:
/lib/router.js
/lib/router.js
this.route('myPage',{
path: '/myPage',
waitOn: function(){ return Meteor.subscribe('myCollection'); },
data: function(){ return MyCollection.find(); }
});
如何实现向客户端发送非响应式数据?
How would I achieve the sending of non-reactive data to the client?
推荐答案
Meteor Pubs/Subs 用于数据反应性.如果您不需要反应性而是服务器为您计算并发送回的一些一次性数据,您需要一个 方法!
Meteor Pubs/Subs are made for data reactivity. If you don't need reactivity but some one-shot data the server computes for you and sends back, you need a method!
// Server code
Meteor.methods('getComplexData', function() {
var complexData = { /* make your complex data */ };
return complexData;
});
// Client code
Meteor.call('getComplexData', function(err, data) {
if(err) {
// Handle error
}
else {
Session.set('complexData', data);
}
});
这篇关于Meteor:如何发布自定义 JSON 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!