I’m finished up a small test case for passing values between Templates, now using both Session and ReactiveVar. I’ll post the full code and explain my rational after I figure out a few details.
The following is the Message Details Template subscription and helper function:
Template.user_messages.onCreated(function () {
var instance = this;
instance.user_id = new ReactiveVar(Session.get('selectedUser'));
instance.messages = new ReactiveVar({});
instance.autorun(function () {
var user_id = instance.user_id.get();
instance.subscribe('user_messages', user_id);
});
});
Template.user_messages.helpers({
user_messages: function () {
var user_id = Template.instance().user_id.get();
return user_messages.find({user_id: user_id}, { sort: { posts: 1 } });
}
});
Will someone please explain to me why, when I open two different browsers in the console, and select two different users, I see two different values for Session?
I thought Session was global and instance transendant – am I wrong? Can this be because I’m setting the ReactiveVar to the Session value on each instance?
Browser1
> Session.get('selectedUser')
< "123"
Browser2
> Session.get('selectedUser')
< "456"
I’m finished up a small test case for passing values between Templates, now using both Session and ReactiveVar. I’ll post the full code and explain my rational after I figure out a few details.
The following is the Message Details Template subscription and helper function:
Template.user_messages.onCreated(function () {
var instance = this;
instance.user_id = new ReactiveVar(Session.get('selectedUser'));
instance.messages = new ReactiveVar({});
instance.autorun(function () {
var user_id = instance.user_id.get();
instance.subscribe('user_messages', user_id);
});
});
Template.user_messages.helpers({
user_messages: function () {
var user_id = Template.instance().user_id.get();
return user_messages.find({user_id: user_id}, { sort: { posts: 1 } });
}
});
Why use ReactiveVars at all for passing data between Templates?
In the following case the Session is just a reactive as above:
Template.user_messages.onCreated(function () {
var instance = this;
instance.autorun(function () {
instance.subscribe('user_messages', Session.get('selectedUser'));
});
});
Template.user_messages.helpers({
user_messages: function () {
return user_messages.find({user_id: Session.get('selectedUser')}, { sort: { posts: 1 } });
}
});
If there’s worry about, after moving to another screen (in my case Template), and having the Session variable sitting around with a value that’s no longer needed, we can always delete it after the Template is flushed:
Template.user_messages.onDestroyed(function () {
Session.set('selectedUser', null);
});
In the 1.1.0.2 Meteor Docs, it states one difference between Session ReactiveVars is:
ReactiveVars don’t have global names, like the “foo” in Session.get(“foo”). Instead, they may be created and used locally, for example attached to a template instance, as in: this.foo.get().
For the purposes of passing data from one Template to another, I can’t see this as having much value.
If this is this one of the main selling points, I think in terms of passing data from Template to Template, I agree with @awatson1978, using Session seems like it is the way to go.