William T Wild
William T Wild

Reputation: 1032

Using $.post and passing return function as variable with additional parameters

I have a main post function that I use for all my post calls:

function post_json(post_string, response_handler) {
    //
    // do logging and other things with post string
    //
    $.post(post_string,{},response_handler,'json');
}

I then will use this call in various pages:

post_json(post_url, handler_generic); 

The handler_generic function is this:

function handler_generic(json) {       
    var success = json.success;
    var success_verbiage = json.success_verbiage;
    // do lots of stuff here   
}

This works perfectly.

What I want to do is have a function like this:

function handler_generic_with_extra(json, unique_id) {       
    var success = json.success;
    var success_verbiage = json.success_verbiage;
    // do lots of stuff here and now do it with the 
    // unique id  
}

I figured these would not work, and they dont:

 post_json(appended_post_string, handler_generic_with_extra( the_unique_id));

 post_json(appended_post_string, handler_generic_with_extra( json, the_unique_id));  

Without creating a new post_json function to handle these cases is there any way to accomplish this?

Upvotes: 0

Views: 32

Answers (1)

The One
The One

Reputation: 4686

Use a closure:

post_json(appended_post_string,function(json){return handler_generic_with_extra( json, the_unique_id); }); 

Upvotes: 1

Related Questions