R
rocketeer
I've a set of Javascript classes that maintain state. For example,
gm.js might be:
var GroupManager { groups: {} };
Over time I add new groups to the list:
GroupManager.groups[key] = myGroup;
If I include another Javascript file that also includes a reference to
include gm.js, or do this through an Ajax call, this new loading of
gm.js overwrites my existing GroupManager variable and I lose all my
accumulated data.
So I thought I could do
if(!GroupManager) { GroupManager = { groups: {} }; }
But the GroupManager so instantiated lives only within the if()
block. And to include an initial var GroupManager simply kills state.
Are there any techniques for allowing multiple includes of a
Javascript file while detecting a previous include, allowing me to
preserve any existing variables?
Thanks,
Jerome
gm.js might be:
var GroupManager { groups: {} };
Over time I add new groups to the list:
GroupManager.groups[key] = myGroup;
If I include another Javascript file that also includes a reference to
include gm.js, or do this through an Ajax call, this new loading of
gm.js overwrites my existing GroupManager variable and I lose all my
accumulated data.
So I thought I could do
if(!GroupManager) { GroupManager = { groups: {} }; }
But the GroupManager so instantiated lives only within the if()
block. And to include an initial var GroupManager simply kills state.
Are there any techniques for allowing multiple includes of a
Javascript file while detecting a previous include, allowing me to
preserve any existing variables?
Thanks,
Jerome