Q:

Can I reset a Meteor Session variable to it's Default value?

I want to reset a Meteor Session variable to it's default value - is that possible?
Example:
Session.setDefault 'foo', 'bar'
Session.get 'foo' # => 'bar'
Session.set 'foo', 'boing'
Session.get 'foo' # => 'boing'
delete Session.keys['foo']
Session.get 'foo' # => undefined

But I want something like resetToDefault which would then do (after the above):
Session.get 'foo' # => undefined
Session.resetToDefault()
Session.get 'foo' # => 'bar'

A:

I came up with this code :
function MyReactiveDict(keys){
    ReactiveDict.call(this,keys);
    this.defaults={};
}
MyReactiveDict.prototype=Object.create(ReactiveDict.prototype);

_.extend(MyReactiveDict.prototype,{
    setDefault:function(key,value){
        ReactiveDict.prototype.setDefault.call(this,key,value);
        this.defaults[key]=value;
    },
    resetToDefault:function(key){
        this.set(key,this.defaults[key]);
    }
});

MySession=new MyReactiveDict();

Session is an instance of ReactiveDict, so what I did was defining our own MyReactiveDict by inheriting the regular ReactiveDict, add the functionality we want, and finally declare MySession as an instance of MyReactiveDir.
MySession would pass the tests you described in your answer.
I think you can even safely overwrite Session in your app by preserving already defined keys with the following code :
Session=new MyReactiveDict(Session.keys);

That's because the ReactiveDict constructor can take an array of existing keys to start with.
You need to meteor add reactive-dict to your application for this code to work.

