ajax 调用成功后渲染部分内容?

发布于 2024-10-24 11:15:34 字数 655 浏览 5 评论 0原文

我有这个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';
    }
 }

这是视图中的部分调用,

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

我只想执行fb_iframe,但是有没有办法渲染部分或重新检查unless块因为 ajax 调用会更新它,以便 unless 条件为 true...有关如何实现一种或任何方式执行此操作的任何想法?

I have this 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 the partial call in the view

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不忘初心 2024-10-31 11:15:34

假设您的部分已经在视图上(因为您说您想要“重新检查除非块”),看起来 Ajax 调用成功后重新加载到页面是您的解决方案。

$.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';
    }
  }
});

Assuming your partial is already on the view (since you said you wanted to 'recheck the unless block'), looks like a reload to the page after the Ajax call succeeds 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';
    }
  }
});
稳稳的幸福 2024-10-31 11:15:34

问题是,当 JavaScript 在客户端运行时,该部分位于您的服务器上。您需要将渲染的 html 从服务器返回给客户端。如何执行此操作取决于该部分是否取决于您的用户模型。

我能看到的两个最佳选项是:

  1. 将部分作为 /update_fb_use 返回的 json 结构的一部分返回。然后说 $("#fb_iframe").html(data.html)

  2. 使用 jQueryMustache.js 。然后,如果您有用户,只需渲染模板即可。

方法 2 的问题在于,在从用户那里获得足够的数据之前,可能很难创建 JavaScript 模板。这完全取决于你的部分。

Here is the problem, the partial lives on your server while the JavaScript is run client side. You need to get the rendered html from your server back to the client. How you do this depends on whether the partial depends on your user model.

The two best options I can see are:

  1. Return the partial as part of the json structure the /update_fb_use returns. Then say $("#fb_iframe").html(data.html).

  2. Include the partial in the original page as a JavaScript template using jQuery or Mustache.js. Then, if you have a user, simply render the template.

The problem with approach 2 is that it may be hard to create a JavaScript template until you get enough data from the user. It all really depends on what is in your partial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文