By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

a reference to an object in an event handler

P: 34
how can i send a reference of an object to an even handler. i have used the following technique while i used the xmlHTTPrequest object.

Expand|Select|Wrap|Line Numbers
  1. function object () {
  2. this.getStuff = function () {
  3. if ( (xmlHttp = getXmlHttpRequest() ) !== false) ) {
  4. var url = 'getStuff.php';
  5. xmlHttp.caller = this;
  6. xmlHttp.open(url, 'POST', true);
  7. xmlHttp.onreadystatechanged = function () {
  8. if (xmlHttp.readyState == 4) {
  9. xmlHttp.caller.evenHandler();
  10. }
  11. }
  12. xmlHttp.send(null);
  13. }
  14. }
  15. }
i stored the reference to the object in the xmlHttp object with the code:
Expand|Select|Wrap|Line Numbers
  1. xmlHttp.caller = this;
but now my problem is if i send more than 1 xmlHttp calls, then the first caller object is over written by the second caller object. how can i get past this.
Nov 20 '07 #1
Share this Question
Share on Google+
6 Replies


acoder
Expert Mod 15k+
P: 16,027
How about using an array to store the references?
Nov 20 '07 #2

P: 34
no that doesnt work because the event handler doesnt know which caller is for which even.

i guess i have over looked something here. maybe i should create a different xmlHttpRequest for each object. that will work right.
Nov 20 '07 #3

P: 34
the same happens in this last solution too.

the event doesnt know the xmlHttpRequest that it was called from? any way to keep track of this. to keep track of where or from what it was called?
Nov 20 '07 #4

acoder
Expert Mod 15k+
P: 16,027
You could keep an index for each created object. This would require you to create an object for the requests and increment each time a request is made.
Nov 21 '07 #5

100+
P: 428
xmlHttp.onreadystatechanged= function () {
if (xmlHttp.readyState== 4) xmlHttp.caller.evenHandler(this);


I gotta say, the name object for a method makes me cringe,,,
Nov 21 '07 #6

P: 34
i am gonna put this part on hold for now as its taking too much time.

but i came up with this code upto now:

Expand|Select|Wrap|Line Numbers
  1. if (xmlHttp.busy) {
  2. xmlHttp.queue[xmlHttp.queue.length] = this.functionName;
  3. } else {
  4. xmlHttp.caller = this;
  5. xmlHttp.open(url, 'GET', true);
  6. xmlHttp.onreadystatechange = function () {
  7. if (xmlHttp.readystate == 4) {
  8. // code to handle response here
  9. if (xmlHttp.queue.length > 0) {
  10. xmlHttp.queue[0]();
  11. } else {
  12. xmlHttp.busy = false;
  13. }
  14. }
  15. }
  16. }
now two requests wont be sent simultanously.

You could keep an index for each created object. This would require you to create an object for the requests and increment each time a request is made.
i already have a parent object that stores an array of the objects that makes the call.
Do you mean i have to make an XMLHttpRequest object for each of the objects. When i do this i cant seem to find any reference to the parent of the event (event meaning the this.xmlHttp.onreadystatechange which is a 'xpconnect' something object as on firebug)

Can you explain more with some code?
Nov 21 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.