[mochikit] Can't get 'this' to point to the right object
Hi, I am having another problem with 'this' again. I have to classes (better said, that's how I would like them to behave), EditGrid and EditCell: --- var EditGrid = function (base_url,target_id) { bindMethods(this); this.base_url = base_url; this.target_id = target_id; [...] this._createGrid(); connect(window.document,'onkeydown',this._handleKey); } EditGrid.prototype = { [methods] } var EditCell = function (el,type,special_edit) { bindMethods(this); this.cell = el; this.type = type; this.special_edit = special_edit; info = el.id.split('_'); this.column = info.pop(); this.id = info.pop(); } EditCell.prototype = { [methods...] } --- When both get 'instantiated' from a javascript block on the html-page there is no problem. 'this' refers in both cases to them selfs as an object. But when I try to 'instantiate' one of them from the same javascript file (be it from the bottom line of the script, or calling EditCell from EditGrid), this refers to 'window'. What can I do about this? Thanks, TiNo --~--~-~--~~~---~--~~ 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: Can't get 'this' to point to the right object
On 7/9/07, TiNo [EMAIL PROTECTED] wrote: Hi, I am having another problem with 'this' again. I have to classes (better said, that's how I would like them to behave), EditGrid and EditCell: --- var EditGrid = function (base_url,target_id) { bindMethods(this); this.base_url = base_url; this.target_id = target_id; [...] this._createGrid(); connect(window.document,'onkeydown',this._handleKey); } EditGrid.prototype = { [methods] } var EditCell = function (el,type,special_edit) { bindMethods(this); this.cell = el; this.type = type; this.special_edit = special_edit; info = el.id.split('_'); this.column = info.pop(); this.id = info.pop(); } EditCell.prototype = { [methods...] } --- When both get 'instantiated' from a javascript block on the html-page there is no problem. 'this' refers in both cases to them selfs as an object. But when I try to 'instantiate' one of them from the same javascript file (be it from the bottom line of the script, or calling EditCell from EditGrid), this refers to 'window'. What can I do about this? There's a bug with the interaction between bind and connect. You either need to upgrade to MochiKit from SVN, or you need to write the connect as: connect(window.document,'onkeydown',this, '_handleKey'); -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: Can't get 'this' to point to the right object
Post us an example please.. Arnar On 7/9/07, TiNo [EMAIL PROTECTED] wrote: Sorry, you told me this before. I have the svn version from a month ago or so. Even without using the connectcall (just istantiating in the same .js file) it goes wrong 2007/7/9, Bob Ippolito [EMAIL PROTECTED]: There's a bug with the interaction between bind and connect. You either need to upgrade to MochiKit from SVN, or you need to write the connect as: connect(window.document,'onkeydown',this, '_handleKey'); -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] 'this' and addCallbacks
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? (MochiKit 1.3.1) Rgds, 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 -~--~~~~--~~--~--~---
[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 -~--~~~~--~~--~--~---