[mochikit] Re: 'this' and addCallbacks
On 7/9/07, kael [EMAIL PROTECTED] wrote: This doesn't work. When the callback, dataReciever, is called it doesn't know what 'this' is any more. I want dataReciever to update the object and then call this.showRecord. SBP.prototype.dataReceiver = function(data) { // data is an array of (id,text) things alert(this.divID + ' dr') // here, this.divD is undefined this.data=data; this.idx = 0; this.showRecord(idx); }; SBP.prototype.refreshRecords = function (){ epReq=loadJSONDoc(this.refreshJSON); epReq.addCallbacks(this.dataReceiver,alertFailed); alert(this.divID) // this.divD here is correct }; Does anybody care to point out the problems in the design? Two major problems in the design. 1. Your code expects something asynchronous to happen as soon as the next line of code executes. Asynchronous code by definition doesn't work like that. Even if the code wasn't otherwise broken, alert(this.divID) will never work where you put it. You can only access it after the callback has happened, which means you either make a second callback or you add it to the end of the code in your existing callback. 2. JavaScript doesn't have bound methods. You can't use this.dataReceiver as a callback. You can use method(this, dataReceiver) though, which will do the right thing. -bob --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups MochiKit group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: 'this' and addCallbacks
On 7/9/07, Bob Ippolito [EMAIL PROTECTED] wrote: On 7/9/07, kael [EMAIL PROTECTED] wrote: This doesn't work. When the callback, dataReciever, is called it doesn't know what 'this' is any more. I want dataReciever to update the object and then call this.showRecord. SBP.prototype.dataReceiver = function(data) { // data is an array of (id,text) things alert(this.divID + ' dr') // here, this.divD is undefined this.data=data; this.idx = 0; this.showRecord(idx); }; SBP.prototype.refreshRecords = function (){ epReq=loadJSONDoc(this.refreshJSON); epReq.addCallbacks(this.dataReceiver,alertFailed); alert(this.divID) // this.divD here is correct }; Does anybody care to point out the problems in the design? Two major problems in the design. 1. Your code expects something asynchronous to happen as soon as the next line of code executes. Asynchronous code by definition doesn't work like that. Even if the code wasn't otherwise broken, alert(this.divID) will never work where you put it. You can only access it after the callback has happened, which means you either make a second callback or you add it to the end of the code in your existing callback. Sorry, I misread the code. It looked like you were setting the divID in the callback. Ignore this part. 2. JavaScript doesn't have bound methods. You can't use this.dataReceiver as a callback. You can use method(this, dataReceiver) though, which will do the right thing. This still applies though. -bob --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups MochiKit group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: 'this' and addCallbacks
2. JavaScript doesn't have bound methods. You can't use this.dataReceiver as a callback. You can use method(this, dataReceiver) though, which will do the right thing. Thanks a lot Bob that works great. -Kael --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups MochiKit group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---