Category: Javascript

Javascript For Living?? Oh hell yeah….

I just came across this article in Ajaxaian where Dion Almaer rant what you need to do when you code JS for living!!! Some says that pure JS is nothing, some says that just JS cant take you anywhere. I agree partially with Dion’s thought and wants to add something by myself.

Well, you can do pure JS for living as I am doing right now. Beside lots other tasks in pageflakes, I write huge amount of JS everyweek to develop flakes as a development engineer. Beside that I also maintain the community, study JS in the user submitted flakes and support them to write efficient JS. If you neglect JS as a language, you are doing a big mistake. JS is extremely powerful and you can do things beyond your imagination with it.

I want to add some tips beside dion’s suggestions. You also need to do the following if you really want to live by coding JS.

1. DOM Scripting is a Must, but not all, simple Node operations will do.
2. Interacting with CSS is also a must.
3. Manipulating JSON is more important than XML becoz its much more flexible and lightweight to transfer your data as JSON object.
4. Choose some popular libraries and stick to them. Personally I suggest Prototype (Its a must), jQuery, mooTools and Scriptaculous. I dont like Dojo becoz it seems too heavier to me. (I dont agree with Dino in this issue, I suggest DRY, Dont Repeat Yourself. Why do you have to reinvent the wheel?)
5. AJAX is also a must.
6. Being able to write JS in Object Notation format (he he he, its JSON)
7. keep yourself uptodate by visiting sites which inform you what’s actually going on in this sector.
8. Be aware of browser compatibility issues (Its a must)

And Finally

9. Love JS

Creating Action Queue for AJAX Calls

In ajax, there are several cases where you need to call a bunch of actions synchronously. That means one action must finish before another one called. You can do it by checking the status of ajax request of previous calls and if that was in finished state, you can call next action. This process may be resource extensive if you dont care for checking. So here is a simple actionChain object which will help to put numbers of actions in a chain (or you better say, queue) and call them one of after one smartly. I just demonstrate how it works

<script>
var actionChain = {
curIndex: 0,
actions: new Array(),
fire: function()
{
if (this.curIndex<this.actions.length)
{
alert(this.actions[this.curIndex]);
this.callback();
}
else
{
alert("finished");
}
},
increase: function()
{
this.curIndex++;
},
addAction: function(actionName)
{
this.actions[this.actions.length] = actionName;
},
initialize: function()
{
this.curIndex=0;
this.actions = new Array();
},
callback: function(){}
}

actionChain.callback=checkVar;

function checkVar()
{
actionChain.increase();
actionChain.fire();
}

for(i=1;i<11;i++)
actionChain.addAction("hello"+i);
actionChain.fire();

actionChain.initialize();
for(i=1;i<4;i++)
actionChain.addAction("hello"+i);
actionChain.fire();
</script>

so here you see, an actionChain object is created and then we add some actions. Then we call the fire() method. This fire() method simply invoke callback function with current action in queue. And the call back function just increases the counter. So when the counter reaches at final offset, it will terminate.

Here I show a basic demo, just modifying it a bit you can create your full fledged ajax action chain object ( i mean a proper object which stores the action, their url, parameters and response callback etc…)

Thanks

Dive into greasemonkey

Mark pilgrim wrote the book “Dive into greasemonkey” which is a cool book to start with. If you want to read it online, just visit this url

http://diveintogreasemonkey.org/

For those who are interested to know what is greasemonkey, read this intro taken from that book.

Greasemonkey
reasemonkey is a Firefox extension that allows you to write scripts that alter the web pages you visit. You can use it to make a web site more readable or more usable. You can fix rendering bugs that the site owner can’t be bothered to fix themselves. You can alter pages so they work better with assistive technologies that speak a web page out loud or convert it to Braille. You can even automatically retrieve data from other sites to make two sites more interconnected.

Greasemonkey by itself does none of these things. In fact, after you install it, you won’t notice any change at all… until you start installing what are called “user scripts”. A user script is just a chunk of Javascript code, with some additional information that tells Greasemonkey where and when it should be run. Each user script can target a specific page, a specific site, or a group of sites. A user script can do anything you can do in Javascript. In fact, it can do even more than that, because Greasemonkey provides special functions that are only available to user scripts.