Hi guys, I know this is not the perfect place to ask this question, but I''m struggling with object-oriented javascript and event-handlers and you guys are probably the only ones adept enough to help me out with this. I have this calendar code I hate (and still have to maintain ;) and wanted to change it to use object-orientation. So I have written a JavaScript-class called PopupCalendar no problem. Now I need to use event-handlers with this class, one of the event-handlers you can see down below: org_apache_myfaces_PopupCalendar.prototype._clickhandler=function(){ alert(typeof org_apache_myfaces_PopupCalendar.prototype); alert(org_apache_myfaces_PopupCalendar.prototype.bShow); if (!this.bShow) this._hideCalendar(); this.bShow = false; } You see that I access this in these event-handlers. Well, I''d like to access this - but this in a event-handler is the element of the event, and not the object I want to refer to, which is the instance of my popupCalendar class. How do I get back to the instance? Anyone got a clue? For further info: I register this event-handler like so: this._addEvent(document,"click",this._clickhandler); where addEvent is the following function: org_apache_myfaces_PopupCalendar.prototype._addEvent=function(obj, evType, fn){ if (obj.addEventListener){ obj.addEventListener(evType, fn, false); return true; } else if (obj.attachEvent){ var r = obj.attachEvent("on"+evType, fn); return r; } else { return false; } } regards, Martin
http://wrath.rubyonrails.org/pipermail/rails-spinoffs/2005-December/001532.html -Amol Katdare On 12/18/05, Martin Marinschek <martin.marinschek-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi guys, > > I know this is not the perfect place to ask this question, but I''m > struggling with object-oriented javascript and event-handlers and you > guys are probably the only ones adept enough to help me out with this. > > I have this calendar code I hate (and still have to maintain ;) and > wanted to change it to use object-orientation. > > So I have written a JavaScript-class called PopupCalendar no problem. > Now I need to use event-handlers with this class, one of the > event-handlers you can see down below: > > org_apache_myfaces_PopupCalendar.prototype._clickhandler=function(){ > alert(typeof org_apache_myfaces_PopupCalendar.prototype); > alert(org_apache_myfaces_PopupCalendar.prototype.bShow); > if (!this.bShow) > this._hideCalendar(); > this.bShow = false; > } > > You see that I access this in these event-handlers. Well, I''d like to > access this - but this in a event-handler is the element of the event, > and not the object I want to refer to, which is the instance of my > popupCalendar class. How do I get back to the instance? Anyone got a > clue? > > For further info: > > I register this event-handler like so: > > this._addEvent(document,"click",this._clickhandler); > > where addEvent is the following function: > > org_apache_myfaces_PopupCalendar.prototype._addEvent=function(obj, evType, > fn){ > if (obj.addEventListener ){ > obj.addEventListener(evType, fn, false); > return true; > } else if (obj.attachEvent){ > var r = obj.attachEvent("on"+evType, fn); > return r; > } else { > return false; > } > } > > regards, > > Martin > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
In Prototype, there''s a specialized helper function for exactly that, called bindAsEventListener. You can have a look at dragdrop.js in scriptaculous to see it in action. -Thomas Am 18.12.2005 um 20:41 schrieb Martin Marinschek:> Hi guys, > > I know this is not the perfect place to ask this question, but I''m > struggling with object-oriented javascript and event-handlers and you > guys are probably the only ones adept enough to help me out with this. > > I have this calendar code I hate (and still have to maintain ;) and > wanted to change it to use object-orientation. > > So I have written a JavaScript-class called PopupCalendar no problem. > Now I need to use event-handlers with this class, one of the > event-handlers you can see down below: > > org_apache_myfaces_PopupCalendar.prototype._clickhandler=function(){ > alert(typeof org_apache_myfaces_PopupCalendar.prototype); > alert(org_apache_myfaces_PopupCalendar.prototype.bShow); > if (!this.bShow) > this._hideCalendar(); > this.bShow = false; > } > > You see that I access this in these event-handlers. Well, I''d like to > access this - but this in a event-handler is the element of the event, > and not the object I want to refer to, which is the instance of my > popupCalendar class. How do I get back to the instance? Anyone got a > clue? > > For further info: > > I register this event-handler like so: > > this._addEvent(document,"click",this._clickhandler); > > where addEvent is the following function: > > org_apache_myfaces_PopupCalendar.prototype._addEvent=function(obj, > evType, fn){ > if (obj.addEventListener){ > obj.addEventListener(evType, fn, false); > return true; > } else if (obj.attachEvent){ > var r = obj.attachEvent("on"+evType, fn); > return r; > } else { > return false; > } > } > > regards, > > Martin > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Thanks to the two of you! that was what I was looking for ;) regards, Martin On 12/18/05, Thomas Fuchs <t.fuchs-moWQItti3gBl57MIdRCFDg@public.gmane.org> wrote:> In Prototype, there''s a specialized helper function for exactly that, > called bindAsEventListener. > You can have a look at dragdrop.js in scriptaculous to see it in action. > > -Thomas > > > Am 18.12.2005 um 20:41 schrieb Martin Marinschek: > > > Hi guys, > > > > I know this is not the perfect place to ask this question, but I''m > > struggling with object-oriented javascript and event-handlers and you > > guys are probably the only ones adept enough to help me out with this. > > > > I have this calendar code I hate (and still have to maintain ;) and > > wanted to change it to use object-orientation. > > > > So I have written a JavaScript-class called PopupCalendar no problem. > > Now I need to use event-handlers with this class, one of the > > event-handlers you can see down below: > > > > org_apache_myfaces_PopupCalendar.prototype._clickhandler=function(){ > > alert(typeof org_apache_myfaces_PopupCalendar.prototype); > > alert(org_apache_myfaces_PopupCalendar.prototype.bShow); > > if (!this.bShow) > > this._hideCalendar(); > > this.bShow = false; > > } > > > > You see that I access this in these event-handlers. Well, I''d like to > > access this - but this in a event-handler is the element of the event, > > and not the object I want to refer to, which is the instance of my > > popupCalendar class. How do I get back to the instance? Anyone got a > > clue? > > > > For further info: > > > > I register this event-handler like so: > > > > this._addEvent(document,"click",this._clickhandler); > > > > where addEvent is the following function: > > > > org_apache_myfaces_PopupCalendar.prototype._addEvent=function(obj, > > evType, fn){ > > if (obj.addEventListener){ > > obj.addEventListener(evType, fn, false); > > return true; > > } else if (obj.attachEvent){ > > var r = obj.attachEvent("on"+evType, fn); > > return r; > > } else { > > return false; > > } > > } > > > > regards, > > > > Martin > > _______________________________________________ > > Rails-spinoffs mailing list > > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >-- http://www.irian.at Your JSF powerhouse - JSF Consulting, Development and Courses in English and German Professional Support for Apache MyFaces