如何“转换"通过 Meteor.publish 返回的数据? [英] How to 'transform' data returned via a Meteor.publish?

查看:17
本文介绍了如何“转换"通过 Meteor.publish 返回的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Meteor 集合具有 转换 能力,允许将行为附加到从 mongo 返回的对象.

Meteor Collections have a transform ability that allows behavior to be attached to the objects returned from mongo.

我们希望关闭自动发布,以便客户端无法访问数据库集合,但我们仍然需要转换功能.

We want to have autopublish turned off so the client does not have access to the database collections, but we still want the transform functionality.

我们使用更明确的 Meteor.publish/Meteor.subscribe 或 RPC 机制 ( Meteor.call()/Meteor.methods() ) 向客户端发送数据

We are sending data to the client with a more explicit Meteor.publish/Meteor.subscribe or the RPC mechanism ( Meteor.call()/Meteor.methods() )

当使用 Meteor.Collection 方法直接检索数据时,我们如何让 Meteor 客户端自动应用转换?

How can we have the Meteor client automatically apply a transform like it will when retrieving data directly with the Meteor.Collection methods?

推荐答案

(Meteor 0.7.0.1) - 流星确实允许将行为附加到通过发布/订阅返回的对象.

(Meteor 0.7.0.1) - meteor does allow behavior to be attached to the objects returned via the pub/sub.

这是来自我提交给流星项目的拉取请求.

This is from a pull request I submitted to the meteor project.

Todos = new Meteor.Collection('todos', {
// transform allows behavior to be attached to the objects returned via the pub/sub communication.
      transform : function(todo) {
          todo.update = function(change) {
             Meteor.call('Todos_update', this._id, change);
          },
          todo.remove = function() {
             Meteor.call('Todos_remove', this._id);
          }
         return todo;
     }
});
todosHandle = Meteor.subscribe('todos');

通过todos"主题返回的任何对象都将具有 update() 和 remove() 函数——这正是我想要的:我现在将行为附加到返回的数据上.

Any objects returned via the 'todos' topic will have the update() and the remove() function - which is exactly what I want: I now attach behavior to the returned data.

这篇关于如何“转换"通过 Meteor.publish 返回的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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