0

I have this:

var MyObject = {};

MyObject.doStuff = function(someParam) {
   var webdav = new Webdav("addr","port");    

   var handler = {
      onSuccess: MyObject.Success,
      onError: MyObject.Fail
   }

   webdav.PUT(handler, filename, options);
}

MyObject.Success = function(result) {
    alert('status ' + result.status + result.statusstring);
}

I'm using exo platform javascript library for webdav access (if it matters)

The handler I'm creating will call MyObject.Success if webdav.PUT is done succesfully. How can i send the someParam to that function too?

Put in another way, after a successful or failed operation, I'm interested in doing something with the someParam, depending of the result.

3 Answers 3

1

This may be what you'r looking for: javascript callback function and parameters

or maybe: http://onemarco.com/2008/11/12/callbacks-and-binding-and-callback-arguments-and-references/

var someParam = 'foo';
var handler = {
     onSuccess: function(result) {success(result, someParam);},
     onError: function() { fail(); }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Did you borrow from me, or do we have the exact same style to javascript? :-) Or wait, I just edited mine - and it looks like yours!
1

You should look into javascript objects, and try to contain the code within one scope. Something like this:

var MyObject = {

    var doStuff = function(someParam) {
        var webdav = new Webdav("addr","port");    

        var handler = {
          onSuccess: function(result) {success(result, someParam);},
          onError: function() { fail(); }
        }

       webdav.PUT(handler, filename, options);
    }

    var success = function(result, someParam) {
        alert('status ' + result.status + result.statusstring);
    }

    var fail = function() {}

    this.doStuff = doStuff;
}

var myObj = new MyObject();
myObj.doStuff(param);

1 Comment

with closures, the someParam will be available in the onSuccess handler
1

One simple way to do it, taking advantage of JavaScript closures:

var handler = {
   onSuccess: function(result) { MyObject.Success(result, someParam); },
   onError: MyObject.Fail
}

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.