3

Possible Duplicate:
$.ajax context option

I've got some code that looks like this:

$.post(self.baseUrl + 'api/method', {param1: data}, function (response) { // Do something }); 

I want to pass a reference to the self object through to the callback, which I imagined would be something like this:

$.post(self.baseUrl + 'api/method', {param1: data}, function (response, self) { // Do something }); 

However, it doesn't work like this, the jQuery documentation doesn't show a way that would make this possible and a cursory Google search hasn't turned up anything. Is this possible, and how can I do so?

3
  • what do you wanto to do with that object? Commented Sep 20, 2012 at 11:23
  • @jhonraymos Call the method that contains this code recursively Commented Sep 20, 2012 at 11:30
  • @AndreasNiedermair I've voted against closing this one - it's asking a very similar thing, but the question has come from a different direction. This question needs an answer that explains that there is a context option, while the proposed duplicate is asking how to use that context. Commented Sep 21, 2012 at 7:34

3 Answers 3

5

If you use the $.ajax method you can specify a context:

$.ajax({ type: "post", context: self, data: {param1: data}, success: function (response) { console.log(this); // now 'this' refers to self } }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. that's exactly what I was after.
1

@karim79 shows the best solution. I just want to show some other possible ways

var App = { baseUrl: "http://.../", fetchData: function() { var self = this; $.post(self.baseUrl + 'api/method', { param1: data }, function(data) { self.onDatafetch(data); //or globalDataFetch(data, self); }); }, onDatafetch: function(data) { this.showMsg(); }, showMsg: function() { alert("Success"); } } App.fetchData(); function globalDataFetch(data, object){ // received data and object } 

Comments

0

why do you want to do sth like that?

you could easily use a closure:

var param1 = data; $.post(self.baseUrl + 'api/method', function (data) { // access param1 here }); 

4 Comments

This is inside a method of the object represented by the self variable, and inside the callback I actually call this method recursively, so I need to pass a reference through.
actually you do not need a reference if you are after calling recursively. just remember the paradigm: each variable of a scope is available from every line (in that scope), no matter where it gets declared - this is because the compiler moves the declaration to the top (but not the assignment, which might be done later) - see this example jsfiddle.net/dittodhole/x359K
btw... recursion can be, not in every case, replaced by a stack - so that might be the better option to implement ...
nevertheless: you do not need a context - this can be done with closures! just introduce a new variable (and see my fiddle for further hints) ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.