在rails中自动刷新包含辅助函数和javascript函数的div

时间:2016-04-11 18:11:48

标签: javascript jquery html ruby-on-rails

我想通知用户是否有新邮件 - &gt;所以我创建了一个新的<div>,我在其中调用辅助函数并使用notify js函数显示通知。它是正常工作,但如果我试图运行它说每3分钟。它没有回应......请提供一些指导......提前谢谢。

我的代码:

  application..html中的

div包含div的部分

    <div class="recentHashMailNotify" ><%= render partial: 'layouts/recentHashMailNotify' %></div>
  

加载控制器的JS函数

    <script type="text/javascript">
        $(document).ready(function() {setInterval(function() {
        $('.recentHashMailNotify').load('/application_controller/recentHashMailNotify');
    }, 2000);
});
</script>
  

控制器中的操作

  def recentHashMailNotify
  render :partial => "layouts/recentHashMailNotify"

  

div的部分

<% @recent = notifyHash %>
    <% if @recent.any? %>
        <% @recent.each do |elem| %>
            <script type="text/javascript">
              $(document).ready(function(){
                $.notify({
                    icon: 'pe-7s-science',
                    message: "<%= elem %>"
                },{
                    type: 'info',
                    delay: 0,
                    placement:{
                    from: 'bottom',
                    align: 'right'
                    }
                });
            });
            </script>
        <% end %>    
    <%end%>
  

notifyHash功能

def notifyHash

  @imap = Net::IMAP.new(.....)
  @imap.login(......)

  @imap.select 'folder'

  @recent = Array.new

  count =0
  @imap.uid_search(["RECENT"]).each do |uid|
        header = @imap.uid_fetch(uid, "ENVELOPE")[0].attr["ENVELOPE"]
        sub = header.subject 
        count += 1
        @recent.push(sub) if sub.include? "#"
  end

  @recent.push "You have got #{count} new mail" if count != 0

  @imap.logout
  @imap.disconnect

  return @recent
end

1 个答案:

答案 0 :(得分:1)

可能有更好的方法来做到这一点。创建一个js,其间隔时间为setTimeout,然后调用函数执行ajax请求并获取新消息。将新消息作为json返回并通过notify处理每个消息,然后在函数结束时重置计时器。请注意,在这个示例中,它每15秒刷新一次,这太快了,并且会很烦人:)

你的js可能看起来像

$(document).ready(function() {
  setTimeout(updateNotifications, 15000);
  function updateNotifications() {
    $.ajax({
      url:"/check-messages",
      dataType: "json",
      success:function(msgs) {
        $.each(msgs, function (key, data) {
          $.notify({
            icon: 'pe-7s-science',
            message: data
          },{
            type: 'info',
            delay: 0,
            placement:{
              from: 'bottom',
              align: 'right'
            }
          });
        });
      }
    });
    setTimeout(updateNotifications, 15000);
  };
});

您需要在config/routes.rb

中创建路线
get "/check-messages", to: "users#get_messages"

然后在application_controller.rb中使用“helper”方法,将其移动到users_controller.rb(它不是Rails-esque指向application_controller的路由并且可以破坏事物)。

@imap = Net::IMAP.new(.....)
@imap.login(......)

@imap.select 'folder'

@recent = Array.new

count =0
@imap.uid_search(["RECENT"]).each do |uid|
      header = @imap.uid_fetch(uid, "ENVELOPE")[0].attr["ENVELOPE"]
      sub = header.subject 
      count += 1
      @recent.push(sub) if sub.include? "#"
end

@recent.push "You have got #{count} new mail" if count != 0

@imap.logout
@imap.disconnect

respond_to do |format|
  format.json { render json: @recent.to_json }
end

另外,@ imap是一个实例变量,除非您在视图中需要它,否则可以将其移动到本地(删除@符号)