ajax 调用成功后渲染部分内容?
我有这个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
假设您的部分已经在视图上(因为您说您想要“重新检查除非块”),看起来 Ajax 调用成功后重新加载到页面是您的解决方案。
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.
问题是,当 JavaScript 在客户端运行时,该部分位于您的服务器上。您需要将渲染的 html 从服务器返回给客户端。如何执行此操作取决于该部分是否取决于您的用户模型。
我能看到的两个最佳选项是:
将部分作为
/update_fb_use
返回的json
结构的一部分返回。然后说$("#fb_iframe").html(data.html)
。使用 jQuery 或 Mustache.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:
Return the partial as part of the
json
structure the/update_fb_use
returns. Then say$("#fb_iframe").html(data.html)
.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.