I Have the following function.
function ChangeDasPanel(controllerPath, postParams) {
$.post(controllerPath, postParams, function(returnValue) {
$('#DasSpace').hide("slide", { direction: "right" }, 1000, function() {
$('#DasSpace').contents().remove();
$('#DasSpace').append(returnValue).css("display", "block");
$('#DasSpace').show("slide", { direction: "right" }, 1000);
});
});
};
But I want to be able to call it like this
ChangeDasPanel("../Home/Test", {} ,function (){
//do some stuff on callback
}
How can I implement support for callbacks in my function?
Why don't you use an object that you can pass through to the function. It is much more jQuery like and it saves you having x named parameters which is hard to maintain as it can get unwieldy when you get past 3 params.
e.g
You know that global variables and functions are evil, so why not put your's into the jQuery namespace:
Read this post to get a better understanding: Creating callback functions in jQuery
You can pass functions like any other object in JavaScript. Passing in a callback function is straight-forward, you even do it yourself in the
$.post()
call.You can decide whether you want to have your callback called as part of the
$.post()
callback or on its own.If I understand you correctly, it's as easy as setting another parameter and calling the variable as a function: