I have several links on my Website that make Ajax calls and replace an div with the response. If I click several of these links quickly, it looks like the responses may replace the wrong div. Response times of the server can vary wildly, so responses may arrive out of order. Is there a solution other than always responding with Javascript that replaces the correct div? TIA, Jeffrey -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On May 5, 4:20 pm, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> I have several links on my Website that make Ajax calls and replace an div > with the response. If I click several of these links quickly, it looks like > the responses may replace the wrong div. Response times of the server can > vary wildly, so responses may arrive out of order. Is there a solution other > than always responding with Javascript that replaces the correct div? >How are you choosing which div gets replaced at the moment? Fred> TIA, > Jeffrey-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 5 May 2011 16:20, Jeffrey L. Taylor <ror-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> I have several links on my Website that make Ajax calls and replace an div > with the response. If I click several of these links quickly, it looks like > the responses may replace the wrong div. Response times of the server can > vary wildly, so responses may arrive out of order. Is there a solution other > than always responding with Javascript that replaces the correct div?Don''t forget the first "a" of AJAX is for "asynchronous". How does each call identify which div it''s supposed to update? Could you be gettings DOM IDs duplicated/confused in your calls? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Quoting Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > > On May 5, 4:20 pm, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote: > > I have several links on my Website that make Ajax calls and replace an div > > with the response. If I click several of these links quickly, it looks like > > the responses may replace the wrong div. Response times of the server can > > vary wildly, so responses may arrive out of order. Is there a solution other > > than always responding with Javascript that replaces the correct div? > > > How are you choosing which div gets replaced at the moment? >A link_to_function calls the jQuery code shown below. ''id'' is the primary key of the model for the div surrounding the link. So the div surrounding the link is replaced with the response from the server. function replace_article(id, action) { obj = $j(''#article_''+id); jQuery.ajax({url: ''/articles/''+action+''/''+id, type: ''POST'', data: {_method: ''put''}, success: function(data){obj.replaceWith(data);} }); } Jeffrey -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 05/05/2011 08:41 AM, Jeffrey L. Taylor wrote:> function replace_article(id, action) { > obj = $j(''#article_''+id); > jQuery.ajax({url: ''/articles/''+action+''/''+id, type: ''POST'', > data: {_method: ''put''}, > success: function(data){obj.replaceWith(data);} > }); > } > > > JeffreyLook closely at your function, what is the scope of the variable named obj? Ask yourself what happens when multiple calls to replace article are sharing a global reference to obj because you forgot to say var obj. -- Ramon Leon -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Quoting Ramon Leon <ramon.leon-fDeA0g24QwDby3iVrkZq2A@public.gmane.org>:> On 05/05/2011 08:41 AM, Jeffrey L. Taylor wrote: > >function replace_article(id, action) { > > obj = $j(''#article_''+id); > > jQuery.ajax({url: ''/articles/''+action+''/''+id, type: ''POST'', > > data: {_method: ''put''}, > > success: function(data){obj.replaceWith(data);} > > }); > >} > > > > > >Jeffrey > > Look closely at your function, what is the scope of the variable > named obj? Ask yourself what happens when multiple calls to replace > article are sharing a global reference to obj because you forgot to > say var obj. >Duh. Just read about Coffeescript and how it saves you from this mistake. Didn''t connect it with my own code. Thank you, Jeffrey -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.