Web development is quite fun, but it starts behave stupid when we mess the things. Understanding and separating layers, is a strategy. There are many ways to think developers in tricky ways to do it.
From fundamentals we should know, client / server architecture. That is basically caring what are the things should happen in client and what should happen in server.
Usually I try my best to handle many things in client. If I really need to do something in server I cache it. Those are the building blocks.
I have seen many developers, render client component in server and pass to views. It is a mess .additional overhead to the whole program.
Then someone can ask me, Okay, then how could we populate dropdown from database table?
Here is the answer,
First let’s start by identifying process flow.
CLIENT --> REQUEST --> SERVER --> SCRIPT --> SQL SERVER --> SCRIPT --> SERVER --> RESPONSE --> CLIENT
If we want to achieve best performance definitely we should thoroughly look at the each and every part of this flow.
All DB transactions should happen in server and we should cache the data using sql cache or 3rd party cache layers such memchached or velocity .Then pass the data in user friendly format .Json is ma favorite. Believe me if u have proper caching system and properly indexed dB , that process is super-efficient .
Now here we come to the point, which most developers forget. It is rendering. After u got whatever the data, still need to populate it .
I will use php in this article since it has sweet inbuilt function to encode data to json format .(json_encode)
Ill just write the pseudo code .
1) Select data from DB table
2) Store data in php array ( array (key => value) where key will be the id column and value will be the data column )
3) Now pass your array to inbuilt function name json_encode( $mysweetphparray );
You just have json formatted data string
This will be the format.
{ {1:hasith},{2:Ar} }
Now return your json string .
Get json string to your client , and format json data with in ur callback function.
There are 2 sweet jquery functions available to do this stuff.
1)parseJson
2)stringnify
3)jquery each to loop through data object nodes
Here is the psudo code ,
$.post (URL , dataposttoserver , function(datareturnbyphpfunction){
Var json =parsejson(datareturnbyphpfunction);
$.each(json, function(key, value) {
alert(key + ': ' + value);
});
Please note ill attach working sample later regarding this . But pseudo code or idea is just enough for good engineer to catch up the stuff.
Well done keep it up...
ReplyDeleteRegards,
Bill Gates.
Ohh Thanks Mr.Gates
ReplyDeleteooh thanks man ,,,it's really nice and getting nice examples and explanation from it...
ReplyDeleteCheers ... ! KIT bro
ReplyDelete