jQuery's Ajax methods are great. They are easy to use. You passed the data to server and in callback method you can use the data which server provided for you. This Asynchronous mechanism is great but sometimes you need to access some variables which you set in your main method before callback. Here we are going to discover how it is possible.
Lets divide it to three parts
- Simple Ajax request
- Add predefined variable to callback
- Real world example
Lets start with a simple callback and then add some predefined variable directly to callback from main method.
Simple Ajax request
There is nothing complicated about this, if you are not familiar with jQuery Ajax you can check it at http://docs.jquery.com/Ajax .
-
function simpleRequest(){
-
$.get( 'service.php',
-
function(data){
-
$('#response1').html(data);
-
}
-
);
-
}
function%20simpleRequest%28%29%7B%0A%20%20%20%20%24.get%28%20%27service.php%27%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20function%28data%29%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24%28%27%23response1%27%29.html%28data%29%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%29%3B%0A%7D
then we call this method simply which will show the result in a div with id 'response1'.
-
-
%3Cdiv%20onclick%3D%22simpleRequest%28%29%3B%22%3Eclick%20here%20to%20see%20simple%20request%21%3C%2Fdiv%3E%0A%3Cdiv%20id%3D%27response1%27%3E%3C%2Fdiv%3E
click here to see simple request!
Add predefined variable to callback
Here we are going to send content of the div directly to callback without sending it to server. The solution is using anonymous method in our code.
-
function requestWithPassedVariable(obj){
-
var content = $(obj).html();
-
$.get( 'service.php',
-
function(data){
-
$('#response2').html(data+':'+content);
-
}
-
);
-
}
function%20requestWithPassedVariable%28obj%29%7B%0A%20%20%20%20var%20content%20%3D%20%24%28obj%29.html%28%29%3B%0A%20%20%20%20%24.get%28%20%27service.php%27%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20function%28data%29%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24%28%27%23response2%27%29.html%28data%2B%27%3A%27%2Bcontent%29%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%29%3B%0A%7D
As you see we first assign it to a variable 'content' and then used that variable in our defined anonymous method.
-
-
-
%3Cdiv%20onclick%3D%22requestWithPassedVariable%28this%29%3B%22%3Eclick%20to%20see%20my%20content%20in%20directly%20callback%21%3C%2Fdiv%3E%0A%3Cdiv%20onclick%3D%22requestWithPassedVariable%28this%29%3B%22%3Eclick%20to%20see%20mine%20too%21%3C%2Fdiv%3E%0A%3Cdiv%20id%3D%27response2%27%3E%3C%2Fdiv%3E
click to see my content in directly callback!
click to see mine too!
Real world example
Imagine that you loaded a category to your page including : books and sites. Now you want to load the items whenever user click on each item. Also we want to load each category items one time for better performance. Here is the code
-
$.list = function(obj,type){
-
var obj = $(obj);
-
if(obj.data('list'))
-
return $.display(obj);
-
$.getJSON( 'service.php?type='+type,
-
function(list){
-
// store data to prevent request for loaded data
-
obj.data('list',list);
-
// display list
-
$.display(obj);
-
}
-
);
-
}
-
$.display = function(obj){
-
$('#listResult').html('');
-
$.each( obj.data('list'),
-
function(){
-
$('#listResult').append(this+' ');
-
}
-
)
-
}
%24.list%20%3D%20function%28obj%2Ctype%29%7B%0A%20%20%20%20var%20obj%20%3D%20%24%28obj%29%3B%0A%20%20%20%20if%28obj.data%28%27list%27%29%29%0A%20%20%20%20%20%20%20%20return%20%24.display%28obj%29%3B%0A%20%20%20%20%24.getJSON%28%20%27service.php%3Ftype%3D%27%2Btype%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20function%28list%29%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20store%20data%20to%20prevent%20request%20for%20loaded%20data%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20obj.data%28%27list%27%2Clist%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20display%20list%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24.display%28obj%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%29%3B%0A%7D%0A%24.display%20%3D%20function%28obj%29%7B%0A%20%20%20%20%24%28%27%23listResult%27%29.html%28%27%27%29%3B%0A%20%20%20%20%24.each%28%20%20%20%20obj.data%28%27list%27%29%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20function%28%29%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24%28%27%23listResult%27%29.append%28this%2B%27%3Cbr%2F%3E%27%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%29%0A%7D
What we did was storing each category's response in itself by using .data('list',list). The next time, when we hit the 'List Books' it will not send a new request instead it will load the data locally from its .data('list'). This magic is done by anonymous method.
-
-
%3Cdiv%20onclick%3D%22%24.list%28this%2C%27books%27%29%3B%22%3EList%20Books%3C%2Fdiv%3E%0A%3Cdiv%20onclick%3D%22%24.list%28this%2C%27sites%27%29%3B%22%3EList%20WebSites%3C%2Fdiv%3E
Click to:
Tags: Ajax | JavaScript | jQuery
|