/**
*    @Author: Arash Karimzadeh
*    @Email:
*    @Desciption: Here, I post some of my recent
*    researches. Also you can see my code projects.
*/  
Ajax C # Chrome CMS dateNet Design Patterns includeMany JavaScript jBind Joomla jQuery Plugin rails ror RubyOnRails SQLite xul

+ All tags

Content View Hits : 336493
Bookmark and Share
jQuery: Add variables to Ajax Callback PDF Print
Written by Arash Karimzadeh   
Tuesday, 30 December 2008 19:24

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

  1. Simple Ajax request
  2. Add predefined variable to callback
  3. 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 .



              
  1. function simpleRequest(){
  2. $.get( 'service.php',
  3. function(data){
  4. $('#response1').html(data);
  5. }
  6. );
  7. }

              
 

              
            

then we call this method simply which will show the result in a div with id 'response1'.


              
  1. onclick="simpleRequest();">click here to see simple request!
  • id='response1'>
  • 
                  
     
    
                  
                
    
    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.

    
                  
    1. function requestWithPassedVariable(obj){
    2. var content = $(obj).html();
    3. $.get( 'service.php',
    4. function(data){
    5. $('#response2').html(data+':'+content);
    6. }
    7. );
    8. }
    
                  
     
    
                  
                

    As you see we first assign it to a variable 'content' and then used that variable in our defined anonymous method.

    
                  
    1. onclick="requestWithPassedVariable(this);">click to see my content in directly callback!
  • onclick="requestWithPassedVariable(this);">click to see mine too!
  • id='response2'>
  • 
                  
     
    
                  
                
    
    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

    
                  
    1. $.list = function(obj,type){
    2. var obj = $(obj);
    3. if(obj.data('list'))
    4. return $.display(obj);
    5. $.getJSON( 'service.php?type='+type,
    6. function(list){
    7. // store data to prevent request for loaded data
    8. obj.data('list',list);
    9. // display list
    10. $.display(obj);
    11. }
    12. );
    13. }
    14. $.display = function(obj){
    15. $('#listResult').html('');
    16. $.each( obj.data('list'),
    17. function(){
    18. $('#listResult').append(this+'
      '
      );
    19. }
    20. )
    21. }
    
                  
     
    
                  
                

    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.

    
                  
    1. onclick="$.list(this,'books');">List Books
  • onclick="$.list(this,'sites');">List WebSites
  • 
                  
     
    
                  
                
                

    Click to:
    • List Books
    • List WebSites
     

    Tags: Ajax | JavaScript | jQuery

    Last Updated on Tuesday, 20 January 2009 15:56