5

i was wondering how to implement a callback into this piece of code

MyClass.myMethod("sth.", myCallback);
function myCallback() { // do sth };

var MyClass = {

myMethod : function(params, callback) {

    // do some stuff

    FB.method: 'sth.',
       'perms': 'sth.'
       'display': 'iframe'
      },
      function(response) {

            if (response.perms != null) {
                // How to pass response to callback ?
            } else {
                // How to pass response to callback ?
            }
      });
}

}

9 Answers 9

13

three ways to achieve "// How to pass response to callback ?" :

  1. callback(response, otherArg1, otherArg2);
  2. callback.call(this, response, otherArg1, otherArg2);
  3. callback.apply(this, [response, otherArg1, otherArg2]);

1 is the simplest, 2 is in case you want to control the 'this' variable's value inside your callbackfunction, and 3 is similar to 2, but you can pass a variable number of arguments to callback.

here is a decent reference: http://odetocode.com/Blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Very complete answer... nice!
7

All you have to do is call the callback function the normal way. In this case you would just do callback(response).

var MyClass = {

myMethod : function(params, callback) {

// do some stuff

FB.method: { 'sth.',
   'perms': 'sth.'
   'display': 'iframe'
  },
  function(response) {

        if (response.perms != null) {
            // How to pass response to callback ?
            // Easy as:
            callback(response);
        } else {
            // How to pass response to callback ?
            // Again:
            callback(response);
        }
  });
}

}

Comments

1

Just invoke the passed in function.

callback(response)

Comments

1

You're close... just use the callback. In this instance, you can form a closure.

var MyClass = {

myMethod : function(params, callback) {

    // do some stuff

    FB.method: 'sth.',
       'perms': 'sth.'
       'display': 'iframe'
      },
      function(response) {

            if (response.perms != null) {
                callback(response);
            } else {
                // response is null, but you can pass it the same as you did above - if you want to.  Probably better to have a generic failure handler
                ajaxFailHandler();
            }
      });
}

Comments

1

I think you can simply call callback(response.perms) there. You could also register it as a

member of your class:

  MyClass.cb = callback;

and later call it:

 MyClass.cb(response.perms)

Comments

0
callback.call(null, response);

Comments

0
MyClass.myMethod("sth.", myCallback);
var myCallback = function myCallback() { // do sth }

var MyClass = {

myMethod : function(params, callback) {

    // do some stuff

    FB.method: 'sth.',
       'perms': 'sth.'
       'display': 'iframe'
      },
      function(response) {

            if (response.perms != null) {
                callback();
            } else {
                callback();
            }
      });
}

}

1 Comment

Not sure why you posted this... but you're not passing the response variable to your callback. callback(response); will do the trick - as indicated in the other answers.
-1

You've got a reference to a function now. Just invoke it:

callback(response.perms);

Comments

-3
var callback = function() {

};

Thats it :-)

5 Comments

Wouldn't it be callback(response)?
This does not seem to have any bearing on the question.
Well, I could just call callback() in the dephest method.
@fabian: Exactly, as the other 6 posters suggested :)
fabian, I advise that you disregard this answer. This is not an answer to your question at all - this is completely unrelated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.