Reputation: 907
I have a Meteor helper that is not reactive. How do I detect that a new key has been added to Session so my helper will rerun ?
'sessions': function() { // FIXME: not reactive
return _.keys(Session.keys).sort();
}
Thanks ! :)
Edit: I know that Session itself is reactive. But the keys of the Session dictionary is not. I would like to be able to detect that a new Session key has been added.
Upvotes: 2
Views: 88
Reputation: 16488
You can achieve this by using Tracker
.
The Session
is an instance of ReactiveDict
, and does not have a way of "subscribing" to all of the keys.
Implementing this involves overriding the original set()
method by monkey-patching.
One way of doing this is by creating a Tracker dependency for the entire dict and invalidating it every time a key is set:
import { Tracker } from 'meteor/tracker';
import { Session } from 'meteor/session';
const originalSet = Session.set;
Session.keyDependendy = new Tracker.Dependency();
Session.set = function() {
originalSet.apply(this, arguments);
this.keyDependendy.changed();
}
// in your helper
'sessions': function() {
Session.keyDependendy.depend();
return _.keys(Session.keys).sort();
}
You can also make it more general by patching ReactiveDict
's prototype.
Upvotes: 1