Introduction
This is my second post about the Revealing Module Pattern.
Here is the first post: Handling JavaScript Complexity
Background
Using global values in JavaScript can lead to collisions where you overwrite a function, value or object that is already defined.
When loading third party software, there can be unknown global values that you may not be aware of.
In the framework that I have created, using the Revealing Module Pattern, I have created a solution for this that I will describe in this tip.
Using the Code
When you are writing a complex JavaScript application, you need some settings and values that can be shared between objects and in this case between modules. Because I do not want to expose these values outside my own application, I have created two modules, the settingsModule
that holds immutable values for the application and a valuesModule
that holds mutable values.
Here is the settingsModule
.
( function( mainModule ){
mainModule.settingsModule = function( ) {
var self = this;
self.settings = {
"someValue" : 2,
"anotherValue" : 20
self.construct = function() {
mainModule.getSetting = self.get;
};
self.get = function( setting ) {
if( self.settings[setting] !== undefined ){
return self.settings[setting];
}
Console.log( 'unknown setting: ' + setting );
return false;
};
self.construct();
return {
};
};
})( mainModule );
The settingsModule
adds a function getSetting
to the mainModule
. With this function, the other modules that are bound to the mainModule
can get a value from the settings through the function:
mainModule.getSetting( 'some value' );
Something similar is done with the valuesModule
. Here, I define global values that can be changed and are shared between different modules. There are two differences to the settingsModule
:
- The values can be changed, so there is a
setValue
function. - The values are grouped by a group name.
( function( mainModule ){
mainModule.valuesModule = function( ) {
var self = this;
self.groups = {};
self.construct = function() {
mainModule.addValue = self.addValue;
mainModule.addValueList = self.addValueList;
mainModule.getValue = self.getValue;
mainModule.setValue = self.setValue;
};
self.addValue = function( id, group, value ) {
if( self.groups[group] === undefined ){
self.groups[group] = {};
}
if( self.groups[group][id] !== undefined ){
Console.log( 'add value warning value already exists id: ' + id );
}
}
else {
self.groups[group][id] = value;
}
};
self.addValueList = function( valueList, group ) {
Console.log( 'addValueList: group, :' + group );
for (var key in valueList ) {
self.addValue( key, group, valueList[key] );
}
};
self.getValue = function( id, group ) {
if( self.groups[group][id] !== undefined ){
return self.groups[group][id];
}
Console.log( 'get value error value not found id: ' + id );
return false;
};
self.setValue = function( id, group, value ) {
if( self.groups[group][id] !== undefined ){
self.groups[group][id] = value;
}
else {
Console.log( 'set value error value not found id: ' + id );
}
};
self.construct();
};
})( mainModule );
The groups object of the valuesModule
is used to store the values in json.
Before you can use the settings and values modules, you have to create them in your main module.
Create the modules in the mainModules construct
function:
( function() {
window.mainModule = new function(){};
self.settings = null;
self.values = null;
self.construct = function() {
self.settings = new mainModule.settingsModule();
self.values = new mainModule.valuesModule();
};
})( mainModule );
Notice that I created a function to add a value or a list of values. The reason for creating these functions instead of just setting the values like with the settings needs some extra explanation that I will discuss in my next post, "Creating a basic project".
History
- 20th November, 2015: Initial version