流星:如何发布自定义JSON数据? [英] Meteor : how to publish custom JSON data?

查看:87
本文介绍了流星:如何发布自定义JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的解决方案是@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 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);
  }
});

有关会话的更多信息

这篇关于流星:如何发布自定义JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆