Skip to content

Instantly share code, notes, and snippets.

@lukemt
Created September 16, 2014 03:39
Show Gist options
  • Save lukemt/bca8fc9b7f28ce923c42 to your computer and use it in GitHub Desktop.
Save lukemt/bca8fc9b7f28ce923c42 to your computer and use it in GitHub Desktop.
Meteor SessionVar (Store a ReactiveVar as a Session-Variable)
SessionVar = function(key, initialValue){
if (! (this instanceof SessionVar))
// called without `new`
return new SessionVar(key, initialValue);
this.key = key;
// check if already defined
if( SessionVar.keys.indexOf(key) !== -1 )
console.log('SessionVar(' + key + ') defined twice!');
SessionVar.keys.push(key);
// set initial value
if( typeof initialValue !== 'undefined' && typeof this.get() === 'undefined' )
this.set(initialValue);
}
SessionVar.prototype.get = function (){
return Session.get(this.key);
}
SessionVar.prototype.set = function (value){
return Session.set(this.key, value);
}
SessionVar.prototype.toString = function () {
return 'SessionVar{' + this.get() + '}';
};
SessionVar.keys = [];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment