Meteor: ReactiveVar vs ReactiveDict

徘徊边缘 提交于 2019-12-07 12:44:59

问题


I understand that the difference between using ReactiveVar and Session variables is, at the basic level, a matter of local vs global variables. ReactiveDict is like a local Session object.

But why use ReactiveDict at all if you're already using ReactiveVar? ReactiveDict doesn't seem to have any advantages over ReactiveVar


回答1:


Here are the main differences between ReactiveDict(RD) and ReactiveVar(RV):

  1. As its name implies, RD offers dictionary-like semantics: set takes a key/value pair and it has delete and clear methods. The bottom line is it lets you cluster reactive data into a single variable. Frankly, this isn't all that interesting in most use cases, but keep reading.
  2. RD has an all method which returns all of the key/value pairs and registers a reactive dependency on each of them. The main benefit is that if you have a cluster of related reactive data you can fire an autorun whenever any part of the dictionary changes. To do this with a set of RVs, you'd need to call get on each of them.
  3. If you pass a name into the RD constructor, it will survive a hot code push. In fact, Session is actually just an RD.

In practice, I've found a few use cases for RDs, but RVs are what you'll likely choose in most situations.

Recommended reading: the ReactiveDict source code




回答2:


"... a general-purpose reactive datatype for use with tracker."

"If you provide a name to its constructor, its contents will be saved across Hot Code Push client code updates."

source: https://atmospherejs.com/meteor/reactive-dict



来源:https://stackoverflow.com/questions/33510951/meteor-reactivevar-vs-reactivedict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!