mathis.hoffmann-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2008-May-16 12:12 UTC
Ajax-Request in an Object
Hello, I have an Object which looks like that: var Newsletterform = Class.create ({ /** * kontructor */ initialize: function (id_form, id_container) { this.form = $(id_form); this.container = $(id_container); Element.show (this.container); Event.observe(this.form, "submit", this.FormSubmit, false); }, /** * Ajax-Success-Handler */ FormSubmitSuccess: function (resp, jsonObj) { alert (''bin hier!''); window.alert (resp); }, /** * Event-Handler * Called, when user clicks submit */ FormSubmit: function () { new Ajax.Request (AjaxApi, { onSuccess: this.FormSubmitSuccess, <---- here!!!!!! parameters: Form.serialize(''newsletterform'') + ''&cat=1&a=1'', method: ''get'', onFailure: function (resp) { alert (''Error!''); } }); } }); I call this object like this: handler = new Newsletterform (''newsletterform'', ''newsletterform_container''); Now I have trouble with the line marked by "<---- here!!!!!!". When I replace the "this" by "handler" the "FormSubmitSuccess"-function is called. Otherwise it doesn''t work. I know (or guess) that it''s the old scope-problem with javascript but I don''t know how to fix it in this case. I don''t want to use handler because the instance-name could change.. I would be happy to get some help with that! M. Hoffmann --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, "You must remember ''this''..." http://blog.niftysnippets.org/2008/04/you-must-remember-this.html The short version for Prototype users is: Use bind() or bindAsEventLister(): http://www.prototypejs.org/api/function#method-bind http://www.prototypejs.org/api/function#method-bindaseventlistener ...but I think the post is worth a read as it discusses *why* you need to use them... Hope this helps, -- T.J. Crowder tj / crowder software / com On May 16, 1:12 pm, "mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" <mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hello, > > I have an Object which looks like that: > > var Newsletterform = Class.create ({ > > /** > * kontructor > */ > initialize: function (id_form, id_container) > { > this.form = $(id_form); > this.container = $(id_container); > Element.show (this.container); > Event.observe(this.form, "submit", this.FormSubmit, false); > }, > > /** > * Ajax-Success-Handler > */ > FormSubmitSuccess: function (resp, jsonObj) > { > alert (''bin hier!''); > window.alert (resp); > }, > > /** > * Event-Handler > * Called, when user clicks submit > */ > FormSubmit: function () > { > new Ajax.Request (AjaxApi, { > onSuccess: this.FormSubmitSuccess, <---- > here!!!!!! > parameters: Form.serialize(''newsletterform'') + ''&cat=1&a=1'', > method: ''get'', > onFailure: function (resp) { > alert (''Error!''); > } > }); > } > > }); > > I call this object like this: > handler = new Newsletterform (''newsletterform'', > ''newsletterform_container''); > > Now I have trouble with the line marked by "<---- here!!!!!!". When I > replace the "this" by "handler" the "FormSubmitSuccess"-function is > called. Otherwise it doesn''t work. I know (or guess) that it''s the old > scope-problem with javascript but I don''t know how to fix it in this > case. I don''t want to use handler because the instance-name could > change.. > > I would be happy to get some help with that! > M. Hoffmann--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry, forgot to say: Based on the code you posted, you need to do this in at least two places. At a glance, you''ll need bindAsEventListener() for your form submit handler, and bind() for the Ajax.Request success handler. -- T.J. Crowder tj / crowder software / com On May 16, 1:12 pm, "mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" <mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hello, > > I have an Object which looks like that: > > var Newsletterform = Class.create ({ > > /** > * kontructor > */ > initialize: function (id_form, id_container) > { > this.form = $(id_form); > this.container = $(id_container); > Element.show (this.container); > Event.observe(this.form, "submit", this.FormSubmit, false); > }, > > /** > * Ajax-Success-Handler > */ > FormSubmitSuccess: function (resp, jsonObj) > { > alert (''bin hier!''); > window.alert (resp); > }, > > /** > * Event-Handler > * Called, when user clicks submit > */ > FormSubmit: function () > { > new Ajax.Request (AjaxApi, { > onSuccess: this.FormSubmitSuccess, <---- > here!!!!!! > parameters: Form.serialize(''newsletterform'') + ''&cat=1&a=1'', > method: ''get'', > onFailure: function (resp) { > alert (''Error!''); > } > }); > } > > }); > > I call this object like this: > handler = new Newsletterform (''newsletterform'', > ''newsletterform_container''); > > Now I have trouble with the line marked by "<---- here!!!!!!". When I > replace the "this" by "handler" the "FormSubmitSuccess"-function is > called. Otherwise it doesn''t work. I know (or guess) that it''s the old > scope-problem with javascript but I don''t know how to fix it in this > case. I don''t want to use handler because the instance-name could > change.. > > I would be happy to get some help with that! > M. Hoffmann--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
mathis.hoffmann-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2008-May-16 12:43 UTC
Re: Ajax-Request in an Object
OK, I understand the problem itself but am not able to use the bindAsEventListener-Method. I tried that: onSuccess: this.FormSubmitSuccess.bindAsEventListener(this) but that doesn''t work. Why? On 16 Mai, 14:18, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > "You must remember ''this''..."http://blog.niftysnippets.org/2008/04/you-must-remember-this.html > > The short version for Prototype users is: Use bind() or > bindAsEventLister():http://www.prototypejs.org/api/function#method-bindhttp://www.prototypejs.org/api/function#method-bindaseventlistener > > ...but I think the post is worth a read as it discusses *why* you need > to use them... > > Hope this helps, > -- > T.J. Crowder > tj / crowder software / com > > On May 16, 1:12 pm, "mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" > > <mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > Hello, > > > I have an Object which looks like that: > > > var Newsletterform = Class.create ({ > > > /** > > * kontructor > > */ > > initialize: function (id_form, id_container) > > { > > this.form = $(id_form); > > this.container = $(id_container); > > Element.show (this.container); > > Event.observe(this.form, "submit", this.FormSubmit, false); > > }, > > > /** > > * Ajax-Success-Handler > > */ > > FormSubmitSuccess: function (resp, jsonObj) > > { > > alert (''bin hier!''); > > window.alert (resp); > > }, > > > /** > > * Event-Handler > > * Called, when user clicks submit > > */ > > FormSubmit: function () > > { > > new Ajax.Request (AjaxApi, { > > onSuccess: this.FormSubmitSuccess, <---- > > here!!!!!! > > parameters: Form.serialize(''newsletterform'') + ''&cat=1&a=1'', > > method: ''get'', > > onFailure: function (resp) { > > alert (''Error!''); > > } > > }); > > } > > > }); > > > I call this object like this: > > handler = new Newsletterform (''newsletterform'', > > ''newsletterform_container''); > > > Now I have trouble with the line marked by "<---- here!!!!!!". When I > > replace the "this" by "handler" the "FormSubmitSuccess"-function is > > called. Otherwise it doesn''t work. I know (or guess) that it''s the old > > scope-problem with javascript but I don''t know how to fix it in this > > case. I don''t want to use handler because the instance-name could > > change.. > > > I would be happy to get some help with that! > > M. Hoffmann--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
mathis.hoffmann-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2008-May-16 12:49 UTC
Re: Ajax-Request in an Object
Maybe you could just write how I should do that in this case...! On 16 Mai, 14:43, "mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" <mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> OK, I understand the problem itself but am not able to use the > bindAsEventListener-Method. > I tried that: > onSuccess: this.FormSubmitSuccess.bindAsEventListener(this) > but that doesn''t work. Why? > > On 16 Mai, 14:18, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > "You must remember ''this''..."http://blog.niftysnippets.org/2008/04/you-must-remember-this.html > > > The short version for Prototype users is: Use bind() or > > bindAsEventLister():http://www.prototypejs.org/api/function#method-bindhttp://www.prototy... > > > ...but I think the post is worth a read as it discusses *why* you need > > to use them... > > > Hope this helps, > > -- > > T.J. Crowder > > tj / crowder software / com > > > On May 16, 1:12 pm, "mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" > > > <mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > Hello, > > > > I have an Object which looks like that: > > > > var Newsletterform = Class.create ({ > > > > /** > > > * kontructor > > > */ > > > initialize: function (id_form, id_container) > > > { > > > this.form = $(id_form); > > > this.container = $(id_container); > > > Element.show (this.container); > > > Event.observe(this.form, "submit", this.FormSubmit, false); > > > }, > > > > /** > > > * Ajax-Success-Handler > > > */ > > > FormSubmitSuccess: function (resp, jsonObj) > > > { > > > alert (''bin hier!''); > > > window.alert (resp); > > > }, > > > > /** > > > * Event-Handler > > > * Called, when user clicks submit > > > */ > > > FormSubmit: function () > > > { > > > new Ajax.Request (AjaxApi, { > > > onSuccess: this.FormSubmitSuccess, <---- > > > here!!!!!! > > > parameters: Form.serialize(''newsletterform'') + ''&cat=1&a=1'', > > > method: ''get'', > > > onFailure: function (resp) { > > > alert (''Error!''); > > > } > > > }); > > > } > > > > }); > > > > I call this object like this: > > > handler = new Newsletterform (''newsletterform'', > > > ''newsletterform_container''); > > > > Now I have trouble with the line marked by "<---- here!!!!!!". When I > > > replace the "this" by "handler" the "FormSubmitSuccess"-function is > > > called. Otherwise it doesn''t work. I know (or guess) that it''s the old > > > scope-problem with javascript but I don''t know how to fix it in this > > > case. I don''t want to use handler because the instance-name could > > > change.. > > > > I would be happy to get some help with that! > > > M. Hoffmann--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
mathis.hoffmann-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2008-May-16 12:56 UTC
Re: Ajax-Request in an Object
OK, I fixed it. Thanks for your tips! On 16 Mai, 14:49, "mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" <mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Maybe you could just write how I should do that in this case...! > > On 16 Mai, 14:43, "mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" > > <mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > OK, I understand the problem itself but am not able to use the > > bindAsEventListener-Method. > > I tried that: > > onSuccess: this.FormSubmitSuccess.bindAsEventListener(this) > > but that doesn''t work. Why? > > > On 16 Mai, 14:18, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi, > > > > "You must remember ''this''..."http://blog.niftysnippets.org/2008/04/you-must-remember-this.html > > > > The short version for Prototype users is: Use bind() or > > > bindAsEventLister():http://www.prototypejs.org/api/function#method-bindhttp://www.prototy... > > > > ...but I think the post is worth a read as it discusses *why* you need > > > to use them... > > > > Hope this helps, > > > -- > > > T.J. Crowder > > > tj / crowder software / com > > > > On May 16, 1:12 pm, "mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" > > > > <mathis.hoffm...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > Hello, > > > > > I have an Object which looks like that: > > > > > var Newsletterform = Class.create ({ > > > > > /** > > > > * kontructor > > > > */ > > > > initialize: function (id_form, id_container) > > > > { > > > > this.form = $(id_form); > > > > this.container = $(id_container); > > > > Element.show (this.container); > > > > Event.observe(this.form, "submit", this.FormSubmit, false); > > > > }, > > > > > /** > > > > * Ajax-Success-Handler > > > > */ > > > > FormSubmitSuccess: function (resp, jsonObj) > > > > { > > > > alert (''bin hier!''); > > > > window.alert (resp); > > > > }, > > > > > /** > > > > * Event-Handler > > > > * Called, when user clicks submit > > > > */ > > > > FormSubmit: function () > > > > { > > > > new Ajax.Request (AjaxApi, { > > > > onSuccess: this.FormSubmitSuccess, <---- > > > > here!!!!!! > > > > parameters: Form.serialize(''newsletterform'') + ''&cat=1&a=1'', > > > > method: ''get'', > > > > onFailure: function (resp) { > > > > alert (''Error!''); > > > > } > > > > }); > > > > } > > > > > }); > > > > > I call this object like this: > > > > handler = new Newsletterform (''newsletterform'', > > > > ''newsletterform_container''); > > > > > Now I have trouble with the line marked by "<---- here!!!!!!". When I > > > > replace the "this" by "handler" the "FormSubmitSuccess"-function is > > > > called. Otherwise it doesn''t work. I know (or guess) that it''s the old > > > > scope-problem with javascript but I don''t know how to fix it in this > > > > case. I don''t want to use handler because the instance-name could > > > > change.. > > > > > I would be happy to get some help with that! > > > > M. Hoffmann--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---