Meteor:如何发布自定义JSON数据?

Meteor:如何发布自定义JSON数据?,meteor,iron-router,Meteor,Iron Router,编辑:我使用的解决方案是@Kyll的解决方案。 假设我要返回的服务器端对象构建起来“复杂”,需要来自不同集合的不同属性。 我第一次尝试: /server/publications.js Meteor.publish('myCustomDocument', function(){ // suppose here that I need to X.find() different collections // and create a complex Array of JSON da

编辑:我使用的解决方案是@Kyll的解决方案。

假设我要返回的服务器端对象构建起来“复杂”,需要来自不同集合的不同属性。

我第一次尝试:
/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'},
            (...)
        ];
});
它不工作,因为它没有返回光标。我想做的是创建一个文档(或文档数组),它是从不同的集合中构建的。
我不需要观察该文件的变化

我已经为它创建了一个集合:

/collections/myCollection.js

MyCollection = new Meteor.Collection('myCollection');
在客户端,使用iron router,我尝试做的是:

/lib/router.js

this.route('myPage',{
    path: '/myPage',
    waitOn: function(){ return Meteor.subscribe('myCollection'); },
    data: function(){ return MyCollection.find(); }
});
如何实现向客户端发送非反应性数据?

Meteor是针对数据反应性而设计的。如果您不需要反应性,但服务器会为您计算并发送一些一次性数据,那么您需要一个


如果数据不会经常更改,则使用方法可能更有意义。这里也可以使用发布/订阅模式,但您需要手动使用发布“gears”,而不是返回光标或任何内容,如下所示:

Meteor.publish("myCustomPublication", function () {
  // here comes some custom logic
  this.added("myCollection", someUniqueId, someCustomData);
  this.ready(); // without this waitOn will not work
});

你试过流星
方法吗?如果是这样的话,你为什么要用收藏来代替呢?我没有试过,我现在会的。事实上看起来很明显。。。D谢谢,我几分钟后告诉你。:-)您希望其他web应用程序能够获取json数据,还是您正在使用应用程序中的json数据?@Nate我正在使用同一个应用程序中的数据。仅供参考。Mongo,收集已弃用。收集已正确无误。Hi@Kyll,感谢您的回答。我正在构建的复杂数据只需要应用程序的一个页面。所以在会话中设置它不是很“坏”吗?你需要一个不同的结构是的。。。如果你真的担心的话,你可以使用a来控制它的删除,也许在回调中。谢谢你的回答,我想我会使用Meteor方法和会话:)
// Client code
Meteor.call('getComplexData', function(err, data) {
  if(err) {
    // Handle error
  }
  else {
    Session.set('complexData', data);
  }
});
Meteor.publish("myCustomPublication", function () {
  // here comes some custom logic
  this.added("myCollection", someUniqueId, someCustomData);
  this.ready(); // without this waitOn will not work
});