Tuesday, November 20, 2012

Manage you application JS code with “namespaces”

For a while now I get to write rich JS applications. Some of the are small, others are pretty big. With the rising amount of JavaScript code there is a need to organize to be able to navigate there later.
There are some ideas for how to make JS to be roughly O.O language. There are the revealing model pattern, the prototype pattern and some others.
But, I find that except using these patterns there are the need to organize your JS code into namespaces or maybe so called “sections”, these should be intuitive, so let’s say for example you need to execute valid email function the JS code should look like this:
var isValid = myApplication.validations.validateEmail(validationFiled);
You can see here I’ve created a global “namespace” for the application, a validation “namespace” and inside these all the validations are defined, thus, whenever I want to validate something I know exactly where to look, and with the help of advance IDE like Visual Studio/Eclipse I can even get an intellisense.

And now the code itself. In JS you can define an array and use it in two different ways.

First way:

var someArray = [];
someArray[0] = 'someValue';
someArray[1] = 'anotherValue';

The second way:

var someArray = [];
someArray.someMember = 'somevalue';
someArray.someOtherMemebr = 'othervalue';
you can see that the second way may look like something we need. So, as we did with our someArray variable we can also make the variables inside arrays and add some more variables (or functions) to them.

How I do it:

Now for how I use to do it in my projects. You do not have to make exactly as I do, this is only an idea. Your needs may be different than mine.

I start with declaring the global scope namespaces and the sub-modules:
var myApplication = [];

//Here, the constant urls will be held (for example, the AJAX urls to contact my server from JS)
myApplication.urls = []

//Some constant variables can go here to be available everywhere.
myApplication.consts = [];

//Global functions you may have (converters, some global calculations, etc...)
myApplication.functions = [];

//Any validation functions you may have
myApplication.validations = [];

//I sometimes define resources inside JS for better response, it could be a "dictionary" object or an array 
myApplication.resources = [];

//I often work with knockout.js inside my projects, I use the following namespace for any models I may have. 
myApplication.models = [];
After declaring the global scope I usually declare some base information and functional data:
//Most of the application will have a modal dialog window to display messages these two functions are used to show and hide this dialog
myApplication.functions.showModalDialog = function(title, content, options){
};
myApplication.functions.hideModalDialog = function(){
};

//This string will define the base url for different url paths of my project 
myApplication.urls.ajax = 'localhost:8888/myApp/ajax';
myApplications.urls.root = 'localhost:8888/myApp';
It is extremely important to keep your JavaScript code in order. You should also consider using the patterns I’ve mentioned earlier.

I will try to post the O.O principles on my next post in this blog.

P.S

New blog, and first post.
I will be glad for every comment, correction, idea, note on this post. Be merciful on this one though :)

No comments:

Post a Comment