简体   繁体   English

Popover中的Rails Ajax Partial:重新打开Popover时,jQuery更改消失; 更改仅在页面上显示

[英]Rails Ajax Partial in Popover: jQuery changes disappear when popover is reopened; changes only show on page reload

I've got a partial I am loading into to the data content for a popover. 我有一部分要加载到弹出窗口的数据内容中。 In the popover, I have a ajax delete action for a list element in the partial that re-renders the new updated partial. 在弹出窗口中,我对部分中的列表元素执行了ajax删除操作,以重新呈现新的更新后的部分。 However, when the popover is closed and re-opened, the jQuery changes made by the delete action disappear, and the partial looks as though the delete never happened. 但是,当关闭弹出窗口并重新打开该弹出窗口时,由delete操作所做的jQuery更改将消失,并且部分看上去好像从未发生过删除操作。 Why? 为什么?

My action: 我的动作:

def destroy
  @notification = Notification.find(params[:id])
  @notification.destroy
  @notification.save

  @notifications = current_user.notifications
  respond_to do |format|
    format.js { render template: "notifications/destroy", :layout => false }
  end
end

My destroy.js.erb : 我的destroy.js.erb

$('div#notifications-list').html($("<%= j render partial: 'notifications/notifications', locals: { notifications: @notifications } %>"))

The popover is called when this link is clicked; 单击此链接时将调用弹出窗口。 this forwards to the action index : 这转发到动作index

<li><%= link_to current_user.notifications.where(read: false).count.to_s, notifications_path, :class => "glyphicon glyphicon-flag notifications", :title => 'Notifications', "data-content" => "#{render   partial: 'notifications/notifications', locals: { notifications: current_user.notifications }}" , rel:   'popover', 'data-toggle'=>"popover", id: 'notifications', remote: true %>
</li>

index action: index动作:

def index
  @notifications = current_user.notifications

  @notifications.each do |notification|
    notification.update_attribute(:read, true)
  end

  @unread_count = @notifications.where(read: false).count

  respond_to do |format|
    format.js { }
  end
end

index.js.erb : index.js.erb

$('#notifications').text('<%= j @unread_count.to_s %>');

And the popover is called in a .js file in the asset pipeline, here: 然后在资产管道的.js文件中调用弹出窗口,如下所示:

$(document).ready(function() {
  $("a#notifications").popover({ 
    trigger: 'click',
      'container': "body",
      'placement': "bottom",
    html: true}).on("show.bs.popover", function(){ $(this).data("bs.popover").tip().css("max-width",   "300px"); 
  });
});

The development log says the destroy.js.erb is rendered fine, same with the index action. 开发日志显示destroy.js.erb呈现良好,与index操作相同。 But for whatever reason, until the page reload the jQuery that was done in the delete action doesn't show.I've tried many things but I really can't figure out why this isn't working. 但是无论出于什么原因,直到页面重新加载在delete操作中完成的jQuery都不显示为止,我已经尝试了很多东西,但我真的不知道为什么这不起作用。 Can someone please help? 有人可以帮忙吗?

I found the solution and have posted it here in case anyone has a similar problem. 我找到了解决方案,并将其发布在这里,以防有人遇到类似问题。 Just had to replace the div with the updated @notifications from the index action in index.js.erb : 只需将index.js.erb index操作的div替换为更新的@notifications即可:

$('div#notifications-list').html($("<%= j render partial: 'notifications/notifications', locals: { notifications: @notifications } %>"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM