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

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

问题描述

流星集合具有转换功能,该功能允许将行为附加到从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?

推荐答案

(流星0.7.0.1)-流星确实允许将行为附加到通过pub/sub返回的对象上.

(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天全站免登陆