Sunday, January 13, 2008

Prototype 1.6.0

Just recently, I have tried to incorporate Prototype library into some of my old javascript source codes and I found the relative ease of using it. I used to spend a lot of time trying to have compatibility between browsers and while functions can easily be replicated in other browsers, the look and feel has always been too cumbersome.

For example, to grab the element that triggers the event, you can just call Event.element(event) and acquired the object you wanted.

Prototype provides a lot of useful functions that you don't worry much whether you are running mozilla, firefox or any others browser. If encountering some compatibility issues, you can still get quirky by running series of Try.these functions to hit a supported feature.

I got an old tree nodes that I customized for an online storage web application. Manipulating these nodes is a breeze when using the library.

Some examples:

1. Finding nodes/nodes:

To traverse allNodes array, a new array function 'find' can do the job elegantly avoiding the use of for/if statements in your code.

var allNodes = [];

function findNodeById(id) {
return allNodes.find(function(n) {
return (node.id == id);
}
}

function findChildNodes(parentId) {
return allNodes.findAll(function(n) {
return (node.pid == parentId);
}
}

2. Declaring nodes

var Node = Class.create();
Node.prototype = {
initialize: function(id, pid, name, title, open, fileType, hasChildren){
this.id = id;
this.pid = pid;
this.name = name;
this.title = title;
this.icon = '';
this.iconOpen = '';
this.fileType = fileType;
this._io = open || false;
this._is = false;
this._ls = false;
this._hc = hasChildren;
this._ai = 0;
this.indents = [];
}
}

Why Bill Gates is Reckoned to be the Greatest Gift to Secular Humanity

No comments: