Wednesday, September 10, 2008

JavaScriptMVC, Singleton Pattern ....

It has been a while since I last posted, and now I'm yet to be a Java certified guy :) 

AJAX has and still is occupying more than 60% of my life. I recently touchbased again with JavaScriptMVC honcho, and he seem to be one of the very qualified person to talk to regarding JS. Inspired as you may put it, I'll try my best to put this blog into use as a media for my recently learned techniques, work-arounds, etc. for the purpose of giving back to the tech gurus who made my days every-now-and-then, and paying forward as well.

Starting of with JavaScriptMVC coupled with Singleton pattern. Make sure those of you guys who are into AJAX applications, starting or already doing production level should look into JavaScriptMVC and tandem it with singleton pattern. Honestly, I owe this two best-practice principle when it comes to the organized AJAX application we currently have. We were able to separate the three parts, model, view, and controllers, though with a little bit of tweeks to fit our requirements. I hope to give some details later for more clarified discussion.

Regarding singleton as described by Pro JavaScript Design Pattern 2008: "The singleton is one of the most basic, but useful, patterns in JavaScript, and one that you will probably use more than any other. It provides a way to group code into a logical unit that can be accessed through a single variable. By ensuring that there is only one copy of a singleton object, you know that all of your code makes use of the same global resource. Singleton classes have many uses in JavaScript. They can be used for namespacing, which reduces the number of global variables in your pages. They can be used to encapsulate browser differences through a technique known as branching, which allows you to use common utility functions without worrying about browser sniffing. Most importantly, they can be used to organize your code in a consistent manner, which increases the readability and maintainabil- ity of your pages. This pattern is extremely important in JavaScript, maybe more so than in any other lan- guage. Using global variables in your pages presents a huge risk, and a namespace created with a singleton is one of the best ways to remove those global variables. This alone would make the singleton worth knowing, but this pattern can be used for many different purposes. We cover the most useful ones in this chapter."

In our apps, we made use of three major namespaces, $m, $v, and $c and branching them according to the needed subfunctions. Examples, 

$v.setElementAttribute(elementObj, attribute, newValue);
$m.im.sendMessage();

With this, codes are organized also based on functionality.

No comments: