meteor : how to publish custom JSON data?

标签 meteor iron-router

编辑:我使用的解决方案是@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/

相关文章:

javascript - 铁 :router - "Couldn' t find a template named 'loading' . ..”

javascript - Meteor UI 异常 : Error: Illegal HTML attribute name:\

javascript - 如何在 meteor 中使用 javaScript 将字符串放入粘贴缓冲区中?

email - Meteor,关于验证邮件的问题

javascript - 在 Meteor 中跨路由传递文件

javascript - 通过 meteor 方法通过点击功能更新子文档被证明是有问题的

javascript - Meteor JS Iron Router,路由子目录

javascript - Materialise: "if"语句中的下拉菜单不起作用

Meteor 文件上传不工作

javascript - react - 当 onClick 子项时创建一个事件类