Question

I'm using ajax to update a partial, but i would like to get some data back from the server at the same time.

$('<%=escape_javascript  @my_params.to_json%>');
$("#partial").html('<%= escape_javascript(render partial: "view/partial" )%>');

gives me the response with both data and partial rendering in it, but it breaks the Ajax render, so the partial is not rendered.

Using Rails 4, ROR. no errors in the browser or Rails

if i refresh the page i get the result i wanted.

I know this must be easier than i'm making it.

Was it helpful?

Solution 2

I'll leave this here for edification, but my friends told me that they would disavow me if i abused a cookie this way. So i switched my implementation, can't afford to lose any friends. They told me to focus on making the turn around time for the ajax call fast. I still needed to know when the object had completed rendering so i simply added a hidden element in the partial and i set its size to zero and wait until the partial rendering changes the height from zero. It was pretty funny to see the interactions with two tabs open to the web page, even though i was planning to use the session ID avoid crosstalk.

Okay, I decided that for my purposes the best way was to put the data in a cookie and just detect when the cookie changed. This allowed me to change the cookie at the end of the send_data command and both get the data for the image position relative to the other image as well as detect when the send_data completed so i could slide the image into place (over the animated wait image).

it looks like this on the server

   send_data(image.to_blob { self.format = 'png' },
          type: 'image/png',
          disposition: 'inline')
cookies[:duck_data] = { :value => $cookie_data.to_json}

and this on the client side

wait_for_paint=->
  if $.cookie('duck_data') == 'keep waiting'
    setTimeout (->wait_for_paint()),  250
   else
    my_data= $.parseJSON($.cookie('duck_data'))
    do_ajax_response(my_data)

OTHER TIPS

Assuming that you are using js.erb format for rendering ajax page. Inside the view partial, write the code

run your js code here or assign to global js variable
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top