Created
September 16, 2014 03:39
-
-
Save lukemt/bca8fc9b7f28ce923c42 to your computer and use it in GitHub Desktop.
Meteor SessionVar (Store a ReactiveVar as a Session-Variable)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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