- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的应用程序设置,显示 Projects
的列表.我已经删除了 autopublish
打包,这样我就不会将所有内容都发送给客户。
<template name="projectsIndex">
{{#each projects}}
{{name}}
{{/each}}
</template>
autopublish
已打开,这将显示所有项目:
if Meteor.isClient
Template.projectsIndex.projects = Projects.find()
if Meteor.isServer
Meteor.publish "projects", ->
Projects.find()
if Meteor.isClient
Meteor.subscribe "projects"
Template.projectsIndex.projects = Projects.find()
find()
是否准确?方法只搜索从服务器端发布的记录?它一直让我绊倒,因为我觉得我应该只拨打
find()
一次。
最佳答案
收藏、发布和订阅是 Meteor 的一个棘手的领域,文档可以更详细地讨论,以避免 frequent confusion ,有时会被 confusing terminology 放大.
这是Sacha Greif (DiscoverMeteor 的合著者)在一张幻灯片中解释出版物和订阅:
要正确理解为什么您需要调用 find()
不止一次,您需要了解 Meteor 中的收藏、出版物和订阅是如何工作的:
field: value
对) . MyCollection = new Mongo.Collection('collection-name-in-mongo')
MyCollection.find({...})
在他们身上,这将返回一个 cursor (一组记录,带有遍历它们并返回它们的方法)。 Minimongo is essentially an in-memory, non-persistent implementation of Mongo in pure JavaScript. It serves as a local cache that stores just the subset of the database that this client is working with. Queries on the client (find) are served directly out of this cache, without talking to the server.
Meteor.subscribe('record-set-name')
publish
中使用的记录集的名称。打电话。 subscribe()
call 将客户端订阅到记录集 - 服务器集合中的记录子集(例如最近的 100 篇博客文章),每个记录中包含所有字段或字段的子集(例如,只有 title
和 date
)。 Minimongo 如何知道将传入的记录放入哪个集合?集合的名称将是 collection
发布处理程序中使用的参数 added
, changed
, 和 removed
回调,或者如果它们丢失(大多数情况下都是这种情况),它将是服务器上 MongoDB 集合的名称。 _id
.这没有解释清楚,但由 Meteor 文档暗示:
When you subscribe to a record set, it tells the server to send records to the client. The client stores these records in local Minimongo collections, with the same name as the
collection
argument used in the publish handler'sadded
,changed
, andremoved
callbacks. Meteor will queue incoming attributes until you declare the Mongo.Collection on the client with the matching collection name.
added
时会发生什么,
changed
和
removed
, 或者根本不发布处理程序 - 大多数情况下是这样。在这种最常见的情况下,集合参数(不出所料)取自您在第 1 步在服务器上声明的 MongoDB 集合的名称。但这意味着您可以拥有不同名称的不同发布和订阅,以及所有记录将最终出现在客户端的同一个集合中。下降到
的水平顶级字段 , Meteor 负责在文档之间执行集合并集,以便订阅可以重叠 - 发布将不同顶级字段发送到客户端的功能并排在客户端上工作,集合中的文档将是
union of the two sets of fields .
BlogPosts = new Mongo.Collection('posts');
BlogPosts
可以从以下位置获取记录:
// server
Meteor.publish('posts-recent', function publishFunction() {
return BlogPosts.find({}, {sort: {date: -1}, limit: 10});
}
// client
Meteor.subscribe('posts-recent');
// server
Meteor.publish('posts-current-user', function publishFunction() {
return BlogPosts.find({author: this.userId}, {sort: {date: -1}, limit: 10});
// this.userId is provided by Meteor - http://docs.meteor.com/#publish_userId
}
Meteor.publish('posts-by-user', function publishFunction(who) {
return BlogPosts.find({authorId: who._id}, {sort: {date: -1}, limit: 10});
}
// client
Meteor.subscribe('posts-current-user');
Meteor.subscribe('posts-by-user', someUser);
posts
MongoDB 中的集合,通过
BlogPosts
服务器上的集合,并最终在
BlogPosts
在客户端收集。
find()
不止一次——第二次是在客户端,因为来自所有订阅的文档最终会出现在同一个集合中,你只需要获取你关心的那些。例如,要获取客户端上的最新帖子,您只需从服务器镜像查询:
var recentPosts = BlogPosts.find({}, {sort: {date: -1}, limit: 10});
关于javascript - 了解 Meteor 发布/订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19826804/
我正在用 Java 创建一组小部件,用于解码和显示在串行接口(interface)接收到的消息。 消息类型由唯一标识符定义。每个小部件只对特定标识符感兴趣。 如何对应用程序进行编程,以便将消息正确分发
我有以下代码,其中包含多个订阅。我需要实现的是这样的: 订阅activateRoute 以获取用户和产品数据。 返回商品数据后,使用商品数据订阅getSeller服务。 使用返回的卖家数据订阅 get
我已经使用 Fitbit 的 PHP 库 (www.disciplinexgames.com/fitbit) 在我的网站中成功集成了 FitBit api。它工作正常,但我现在想使用订阅 API,以便
在我的 Angular 7 应用程序中,我有下一个功能: getUserData(uid) { return this.fireStore.collection('users').doc(
我正在尝试在 Node 中实现发布/订阅模式,但不使用 Redis。功能应该是相同的;您可以发布到 channel ,订阅 channel 并收听数据(如果您已订阅);以下是 Redis 功能: pu
这是我当前的应用程序结构: /client/client.js /server/server.js collection.js 有 HTML 和 CSS 文件,但这些与我的问题无关。在将我的应用程序拆
我们正在使用OpenTok建立视频聊天室体验,并且在基本工作正常的同时,我发现当 session 室中有很多参与者发布音频时,本底噪声非常高。像Zoom这样的浏览器外解决方案似乎没有这种高水平的“白噪
RabbitMQ 是点对点还是发布-订阅?或者两者都取决于配置选项? 我一直在查看配置,它们似乎都支持点对点模型而不是发布-订阅。即消息一旦被消费就会从队列中删除,并且不可用于第二个消费者。 最佳答案
我是 Angular 6 和 ngrx 商店的新人。我尝试在从商店订阅数据后调度操作,但它会导致无限循环并使浏览器崩溃?我错了什么。我发现它使用 rxjs 的 do/tap 运算符但仍然不起作用的一些
这个问题已经有答案了: Property '...' has no initializer and is not definitely assigned in the constructor (37
这个问题已经有答案了: Property '...' has no initializer and is not definitely assigned in the constructor (37
我正在使用 Visual Studio 2017 v15.6.2 和 Azure Services Authentication Extension 为支持 MSI 的应用程序进行本地 azure 功
我想知道如何确定给定的 WC_Product 对象 $product 是否是订阅产品。 最佳答案 您可以使用他们的辅助函数,这可能是最完整的: if( class_exists( 'WC_Subscr
我正在研究使用服务器发送的事件作为支持 api 来实现“订阅”类型。 我正在苦苦挣扎的是接口(interface),更准确地说,是这种操作的 http 层。 问题: 使用原生 EventSource不
我会根据每个用户的订阅类型向我的用户发送通知。 例如: 用户 A 订阅了所有新闻文章 用户 B 订阅了所有评论 用户 C 订阅了网站上的所有新内容 我有一个每 5 分钟运行一次的脚本(除非该脚本仍在运
我正在使用 Ionic2/Angular2,并且需要使用参数 authData 调用函数,如下所示。 public auth: FirebaseAuth this.auth.subscrib
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我们现有的系统可以持续处理大量文件。粗略地说,每天大约有 300 万个文件,大小从几千字节到超过 50 MB。这些文件从接收到完成使用会经历几个不同的处理阶段,具体取决于它们所采用的路径。由于这些文件
我有一项服务,我使用 Paypal 订阅。 Paypal 有 webhooks。问题是我不知道我需要使用哪个,不知道用户是否为下个月付款。 我使用了 Billing subscription rene
我目前正在为一个网站整理一个处理脚本,遇到了一个我似乎无法找到明确答案的问题。 Paypal 的文档充其量是不确定的,我对 Paypal 的使用还不够多,无法从他们提供的信息中轻松辨别答案。 当通过
我是一名优秀的程序员,十分优秀!