流星和订阅 [英] Meteor and subscriptions

查看:44
本文介绍了流星和订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白Meteor.subscribe 的概念.它应该从服务器接收记录,并将其附加到同名的集合中,对吗?

I don't understand the concept of Meteor.subscribe. It is supposed to receive records from the server, and attach it to collections with the same name, right?

[subscribe] 将对传入的属性进行排队,直到您在客户端上使用匹配的集合名称声明 Meteor.Collection.

[subscribe] will queue incoming attributes until you declare the Meteor.Collection on the client with the matching collection name.

那么,为什么 docs 中的示例使用不同的名称?所有玩家玩家之间是什么关系?

So, why the example in docs uses different names? What is the relation between allplayers and players?

Meteor.subscribe("allplayers");
...
// client queues incoming players records until ...
...
Players = new Meteor.Collection("players");

推荐答案

有两个名字:

  • 集合的名称(在本例中为 'players').
  • 订阅名称(在本例中为'allplayers').

订阅是将记录放入客户端集合的一种方式.记录进入的这个集合的名称(在服务器端)由相关 Meteor.publish 函数中的 this.set() 决定,但是 通常它只是在服务器端查询的集合的名称[1].

A subscription is a way to get records into a client side collection. The name this collection that the records go into is decided (on the server side) by the use of this.set() in the relevant Meteor.publish function, but usually it is just name of the collection that is queried on the the server side[1].

许多订阅可以将数据存入同一个集合中,因此订阅名称当然不需要与集合名称相对应.事实上,如果您对该集合进行相当简单的单一订阅,那么让它们相同可能只是一个好主意.

Many subscriptions can deposit data into the same collection, so certainly the name of the subscription doesn't need to correspond to the name of the collection. In fact, it's probably only a good idea to have them be the same if you are doing a fairly straightforward single subscription to that collection.

[1] 如果您在 Meteor.publish 中返回一个光标(例如 return player.find();),它会自动连线为您调用 this.set(name),其中 name 是从服务器端 players 集合推断出来的.

[1] If you return a cursor (e.g. return players.find();) in Meteor.publish, it automatically wires up calls to this.set(name) for you, where name is inferred from the server side players collection.

这篇关于流星和订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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