2

I'm using a $.ajax function that serves two purposes and depending on the context, I want to execute different functions on the call-back.

function MyAjaxCall(SomeParameter, CallBackFunctionName) {

  $.ajax({
   ...
   success: function (msg) {

     var TheData = msg.hasOwnProperty("d") ? msg['d'] : msg;
     // here: "execute the function in parameter CallBackFunctionName 
     // AND pass it the parameter TheData
  }
}

How do I write the line where the name of the function is a parameter and I want to pass it TheData as the parameter.

Note, at the moment, I'm writing it like that:

if (CallBackFunctionName === "SomeFunctionName1") {
   SomeFunctionName1(TheData);
} else {
   SomeFunctionName2(TheData);
}
1
  • is that function a global function? why don't you just pass the actual function into the MyAjaxCall method? Commented Nov 10, 2012 at 9:43

2 Answers 2

4

If the function is defined as a global function then use :

window[functionName](arguments);

If it isn't then change the way MyAjaxCall is called like so:

MyAjaxCall.apply(thisArg, [SomeParameter, CallBackFunction]); //thisArg is the value of the this object inside MyAjaxCall().

Then inside MyAjaxCall() do this:

function MyAjaxXall(SomeParam, CallBackFunction){
  var me = this; //the object supplied through thisArg while calling.
  $.ajax({
     success : function(msg)
     {
         //whatever processing you want
         me[CallBackFunction](arguments);
     }

  });
}

Or you could add the object as part of the paramters of MyAjaxCall():

function MyAjaxCall(SomeParam, obj, CallBackFunction)
{

      $.ajax({
         success : function(msg)
         {
             //whatever processing you want
             obj[CallBackFunction](arguments);
         }

      });
}

When using it for calling a global function use:

MyAjaxCall(SomeParam, window, CallBackFunction);
Sign up to request clarification or add additional context in comments.

1 Comment

ok, thanks for the answer. Upvoted because of the concept but I'm keeping my implementation because it's simpler. Thanks again.
2

Assuming that the defined function which name is passed via the variable CallBackFunctionName is global, you could do this:

window[CallBackFunctionName](TheData)

You could also just pass the actual function to MyAjaxCall like this:

var MyCallbackFunction = function(data){ console.log(data) }
MyAjaxCall({param1: 'value1'}, MyCallbackFunction)

This way you can just execute the function:

function MyAjaxCall(SomeParameter, CallBackFunction) {

  $.ajax({
   ...
   success: function (msg) {

     var TheData = msg.hasOwnProperty("d") ? msg['d'] : msg;
     CallBackFunction(TheData)
  }
}

2 Comments

Actually, it's all inside a closure and none of these functions are accessible from the global scope.
ok, thanks for the answer. Upvoted because of the concept but I'm keeping my implementation because it's simpler. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.