I am currently developing a comprehensive global data storage system for my application (specifically an Angular JS app - although this question pertains to JavaScript in general).
I have established a 'service' that is responsible for setting the data, retrieving the data, and other related functions.
Here is how it looks:
angular.module('core').factory('dataService', function(callsService) {
let properties = {
globalData: {}
};
properties.insertData = function(data) {
for (let x in data) {
this.globalData[x] = data[x];
}
return;
}
properties.getData = function(data) {
return this.globalData[data];
}
return properties;
});
The usage of the service would be like this:
dataService.insertData({foo: 'bar'});
dataService.getData('foo'); // 'bar'
However, issues arise when dealing with nested data structures, for example:
dataService.insertData({foo: {bar: 'hello world'}});
dataService.getData('foo'); // {bar: 'hello world'}
While this behavior is expected due to object references, how can I achieve something like:
dataService.getData('foo.bar'); // 'hello world'
or
dataService.getData('[foo][bar]'); // 'hello world'
When revisiting my 'properties.getData' method, is there a way to recursively access nested objects or employ another technique?
properties.getData = function(data) {
return this.globalData[data]; // should be able to retrieve nested objects
}