Showing posts with label ajax post. Show all posts
Showing posts with label ajax post. Show all posts

Wednesday, February 2, 2011

How to make a remote call synchronous in Rails

new Ajax.Request('/controller/action?username=praveen', {asynchronous:false, evalScripts:true, parameters:'page_offset=' + page_offset + '&authenticity_token=' + encodeURIComponent('e060znd6JcrMCU6Bf4ZZnfyBac4tl1YXQ6HuEHZGY58=')});

What to do to get that "asynchronous:false" thing in your link_to_remote or any other helper function available in rails :)

I know its not a big thing and there is/are very few requirements to make an Ajax call synchronous but one might face this requirement sometimes, and the answers is
<%=remote_function(:url => {:controller => "controller", :action => "action", :username => "praveen"}, :type => :synchronous, :with => "'page_offset=' + page_offset") -%>

And you are done... so now till the time this thing is taking place, everything in browser will be waiting to complete the process first and then only proceed next.
Hope you stumble on me looking in for me.

Monday, October 4, 2010

How to Stop an Ajax request in prototype.js

Question is what will you do if you are using prototype.js(which is not having any abort method) for making a Ajax request and due to some circumstance you want to abort the ajax request raised. Well one can think that this is a narrow requirement, why will anybody need to do this because in the meantime request will be done and one might be having the response in return from server.

I hit this corner case where page content was loading from an Ajax request and which after rendering was firing another Ajax request to update the paginate-r(which is also self firing the request on response completion), but to my surprise i was not able to control the "Ajax request to update paginate-r" and hence i am creating the self firing request but am not able to destroy them.

And i don;t know for what reason there is no "ajaxRequest.abort()" method. And i found the answer from a narrow corner of http://blog.pothoven.net/2007/12/aborting-ajax-requests-for-prototypejs.html and it worked and just to increase the find-ability of this solution am re-writing the post.

introduce a method abort like this for prototype.js
Ajax.Request.prototype.abort = function(){
this.transport.onreadystatechange = Prototype.emptyFunction(); //prevent state change callbacks from being issues
this.transport.abort(); //abort the XHR
Ajax.activeRequestCount--; //updates the request counter
};

and then in your code
write it somewhere so that it is executed only once
<script>
var cw_paginator_request = null;
</script>

and then to assign the request if null else abort the request and fire a new request, like this
<script>
if(cw_paginator_request != null){
cw_paginator_request.abort();
}
cw_paginator_request = <%=remote_function(:url => {:controller => "some_controller", :action => "update_paginator", :id => @selected_id }) -%>;
</script>

Hopefully it might help somebody in need :)

Thursday, March 26, 2009

send_file or send_data in link_to_remote

Hi guys,

really really long time i did anything here :).. and no this time not an usual bhaana(excuse) will do but an genuine reason.. was busy building something good.. and it is a secret(just can't disclose it here like this).

Anyhow coming to point.. i was doing a general routine development task for my company and i got hit by a situation like this.. where i was making an ajax call to a controller/action and in response i was sending a stream i.e (send_data and send_file) but as the post was xmlhttp everything was working fine but download was not available (and yeh over here i coded it for download not the inline stuff). What to do now.... i searched it ya ya googled it and response was not a solution that i can rely on(it might not be google's fault must be mine, but why am i favoring it.. it must be google fault lol :)) )

So i solved it my own way by redirecting the update to other action and coding the stream in the newly redirected action :)

Here are some snippets of it....

<%=link_to_remote "Export the result",:url => {:controller => "some_controller", :action => "ask_to_export"},:with => "'record_ids=1-2-3-4-5-6'" -%>

def ask_to_export
render :update do |page|
page.redirect_to :action => "process_and_export", :record_ids => params[:record_ids]
end
end

def process_and_export
outfile = ""
CSV::Writer.generate(outfile) do |csv|
csv << ["praveen","kumar","sinha",1,2,3,nil,""]
end
send_data outfile, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition => "attachment; filename=report_#{Time.now.to_f.to_s}.csv"
end

I hope this must be helpful to some one stucked over here and googling it

Thanks :)

Honda Civic & A/C problems.

Hello Friends, Got a post again on to Honda Civic (The good old favorite commuter of mine). This car has been doing great except for so...