Meteor:使用 Sessions 和 ReactiveVar [英] Meteor: using Sessions and ReactiveVar

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

问题描述

什么时候应该使用 SessionReactiveVar?我使用 Session 变量作为组件之间的通信媒介.我们以 Stackoverflow 为例.

When should you use Session and ReactiveVar? I use Session variable as a communication medium between components. Let's look at Stackoverflow for example.

我标记了三个假设的组件.让我们看看 filters 组件.如果您单击 Tags,则 Main 组件将根据您喜欢的标签显示其问题.为此,我将在单击 Tags 按钮时设置 Session.set("Main/showTags", true).在 Main 组件中,我将有一个如下所示的辅助函数:

I labeled three hypothetical components. Let's look at the filters component. If you click Tags then the Main components will show its questions based on your favorite tags. To do this, I will set Session.set("Main/showTags", true) when the Tags button is clicked. In the Main component, I will have a helper function like below:

Template.Main.helpers({
  posts: function() {
    var isTags = Session.get("Main/showTags");
    var isQuestions = Session.get("Main/showQuestions");
    ...
    if (isTags) {
      return Posts.find().sort({tags: 1}) // or something along the lines
    } else if (isQuestions) ...
  }
});

这在大多数情况下效果很好,但我从很多地方看到我应该避免使用 Session 并使用 ReactiveVar.但是如果我在任何地方都使用 ReactiveVar,那么我将需要对所有模板实例的引用.我希望它可以在直接父模板和子模板之间很好地工作(例如,在主要组件内部,可能有 VoteAnswerViewsButtonTemplate)但是当你想要时你会如何使用 ReactiveVar独立组件相互通信?

This has worked well in most cases but I've seen from a lot of places that I should refrain from using Session and use ReactiveVar. But If I use ReactiveVar everywhere, then I would need the reference to all template instances. I expect it to work well between direct parent and children templates (ex. inside the main component, there could be VoteAnswerViewsButtonTemplate) but how would you do it with ReactiveVar when you want independent components to talk to each other?

这是我的最后一个问题.如何适当地使用 SessionReactiveVar 来保持组件的范围并使它们相互通信?另外,如果我像现在这样使用 Session,我是否会不必要地污染全局命名空间?

Here's my final question. How can I appropriately use Session and ReactiveVar to keep the scope of components and make them communicate with each other as well? Also, if I use Session the way I do now, am I polluting the global namespace unnecessarily?

相关文件:

  1. https://dweldon.silvrback.com/scoped-reactivity
  2. https://www.discovermeteor.com/blog/reactivity-basics-meteors-magic-demystified/

推荐答案

据我所知,没有与 Session 变量相关的内置功能可以将它们与常规的响应式字典全局区分开来例如,您将在 client.js@Kyll 所述)> 文件.唯一的区别是 Session 必须在应用程序范围内访问的限制".

As far as I know, there is no built-in features related to Session variables that differentiate them from a regular reactive dictionary global variable (like @Kyll stated) that you would declare, for instance, in your client.js file. The only difference is the Session "limitation" of being necessary accessible application wide.

当我使用反应字典时,我很高兴利用这种差异或反应性变量在较小的范围内.我认为我有三种范围:

I am very pleased to take advantage of this difference, when I use a reactive dictionary or reactive variables in a smaller scope. I consider I have three kind of scopes:

1 - 全球范围.例如.当前的 UI 语言,UI 皮肤.为此,我使用 Session.简单的全局数据,不会与其他数据混淆.

1 - Global scope. E.g. the current UI language, the UI skin. I use Session for that. Simple and global data, not the kind that could be confused with anything else.

2 - 一组模板. 例如,假设我创建了一个页面来在我的应用中生成和自定义 pdf.我不会在其他地方重用任何组件.我的集群是一个包含三个文件的文件夹,我们称它们为 pdfgenerator.htmlpdfgenerator.jspdfgenerator_controller.js.

我使用 pdfgenerator_controller.js 来扩展包含所有细节的路由.

I use pdfgenerator_controller.js to extend the route with all the specifics.

pdfgenerator.js 文件中,我有几个模板,我都在集群中使用.在文件的开头,我创建了一个响应式字典 pageSession(类似于响应式变量)并在我的所有集群中使用它.它允许我在所有组件之间传递数据.

In the pdfgenerator.js file, I have several templates that I all use in the cluster. At the beginning of the file, I create a reactive dictionary pageSession (similar to reactive variables) and I use it in all my cluster. It allows me to pass data among all my components.

3 - 本地范围.无论是单个模板还是可重用的组件,它都旨在单独工作.我也不会为那些使用会话变量.我不想让 Session 名称空间过度拥挤.我所做的是将我在实例化期间操作它所需的所有数据传递给我的模板.

3 - Local scope. Whether it is a single template or a reusable component, it is meant to work alone. I won't use Session vars for those either. I do not want to overcrowd the Session name space. What I do is pass to my template every data I need to operate it during its instantiation.

它可能来自空格键:

{{> mySingleTemplate data=myData}}

或使用 Javascript:

or using Javascript:

Blaze.renderWithData(Template.mySingleTemplate , myData, self.firstNode);

在局部作用域的情况下,我还使用 reactive dictionaryreactive vars 来处理单个模板中发生的反应性.在这种情况下,我会尽量避免需要被动地将数据返回到父模板的情况.如果我必须(即我可能不会用它制作一个包),我会使用在父模板范围内声明为全局的本地 minimongo 集合.这样,我可以将信息从可重用组件传递到其父级.

In the local scope case, I also use a reactive dictionary or reactive vars just to handle the reactivity happening within the single template. In this case, I try to avoid the situations where I need to reactively return data to the parent template. If I have to (i.e. I won't probably make a package out of it), I use a local minimongo collection declared as global in the scope of the parent template. This way, I can pass along informations from the reusable component to its parent.

示例:上传模板.我使用 minimongo 来存储每个上传文件的名称、大小、类型、状态和 url.minimongo 集合在父表单模板和子上传器模板之间共享.

底线:我只使用 Session 变量作为基本和全局信息.如果我需要全局共享的数据结构太复杂/太大,我就依赖一个集合.

Bottom line: I only use Session variables for basic and global information. If the data structure I need to share globally is too complex/large, I rely on a collection.

我很想知道我是否做对了,所以这与其说是测试人们是否同意我的做事方式,不如说是一个答案.欢迎大家提出意见和建议.

I am curious to know if I get it right, so this is as much an answer than a test to see if people agree with my way of doing things. All comments and advices are welcome.

这篇关于Meteor:使用 Sessions 和 ReactiveVar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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