0

Let's suppose I want to create a javascript class/object/function which have a method that can accept a callback object like this:

callbacks = {
   onSuccess : // method to be executed in case of success
   onComplete : // method to be executed in case of complete
   onFailure : // method to be executed in case of failure
   onError : // method to be executed in case of an error
}

So let's suppose the classes name definition are the following:

var Obj = function () {};

Obj.prototype.exec = function (callbacks, event, caller, argument) {
}

And I want to use the object in this way:

var mytest = new Test();
var myObj = new Obj();
Obj.exec(callbacks,["onSuccess", "onComplete"], mytest, arguments);

How should Obj.prototype.exec be implemented?

3
  • Is onSuccess supposed to be one of those callbacks? Commented Sep 21, 2011 at 14:17
  • 1
    That is "An object" not "JSON". Commented Sep 21, 2011 at 14:24
  • Yes, onSuccess is supposed to be one of those callbacks. I corrected the callback object Commented Sep 21, 2011 at 14:27

1 Answer 1

1

It's a little confusing what you are asking:

Obj.prototype.exec = function (callbacks, event, caller, argument) {
    var $xhr;
    if (argument instanceof Array)
        $xhr = caller.ajax.apply(this, argument)
    else
       $xhr = caller.ajax.call(this, argument)

    // wire up the listeners
    for (var i = 0; i < event.length; i++) {
        $xhr[event[i]] = callbacks[event[i]];
    }

}

something like this perhaps

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

Comments

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.