Do partial after successful ajax call?

I have jquery ajax

$.ajax({ url: "/update_fb_user?fb_uid=" + FB.getSession().uid, dataType: 'json', success: function(data){ if(data.user != "none"){ // here is where i need to render this partial }else{ window.location.href = '/signup'; } } 

here is a partial call in the view

 <div id="fb_iframe"> <% unless current_user.not_using_facebook? %> <%= render :partial => 'fb_iframe' %> <% end %> </div> 

I was thinking that I was just fb_iframe , but there is a way to either partially or partially recheck the unless block, because the ajax call updates it, so unless condition unless true ... any ideas on how to achieve one or any way to do this?

+4
source share
2 answers

Assuming your partial is already in the view (since you said you want to "double-check the if block"), it looks like reloading the page after a successful Ajax call - this is your solution.

 $.ajax({ url: "/update_fb_user?fb_uid=" + FB.getSession().uid, dataType: 'json', success: function(data){ if(data.user != "none"){ window.location.reload(); }else{ window.location.href = '/signup'; } } }); 
+4
source

Here is the problem, partial life on your server when JavaScript is running on the client side. You need to return the processed html from the server back to the client. How you do this depends on whether the partial depends on your user model.

The two best options that I see are as follows:

  • Returns partial as part of the json structure returned by /update_fb_use . Then say, $("#fb_iframe").html(data.html) .

  • Include the partial source page as a JavaScript template using jQuery or Mustache.js . Then, if you have a user, just draw a template.

The problem with approach 2 is that creating a JavaScript template can be tricky until you get enough data from the user. It all really depends on what is in your partial.

+3
source

Source: https://habr.com/ru/post/1344420/


All Articles