Piotr Migdal
Piotr Migdal

Reputation: 12792

Storing objects in Meteor sessions

I need to store an object (dictionary) in Meteor session (for example: search criteria), but it is not clear for me what is the best way to do it.

Using Session.set and Session.get works, but is cumbersome, as it appears to copy the whole object:

var dict = Session.get("x");
dict['some key'] = 5;
Session.set("x", dict)

Using global variable does not require copying objects, but does not fire reactivity (e.g. for global variable dict putting dict['some key'] = 5; does not fire templates dependent on dict).

Upvotes: 0

Views: 680

Answers (1)

richsilv
richsilv

Reputation: 8013

You can easily add reactivity to other objects using the Deps package if you need to customise things in a way that's more appropriate for dealing with dictionaries or whatever other structure - have a look at this video. Clearly, the syntax is always going to be minimally more cumbersome than for vanilla variables, but it's perfectly possible to make things pretty concise this way.

Upvotes: 1

Related Questions