2016-04-11 19 views
2

新しいメールがある場合にユーザーに通知したい - ><div>を作成し、ヘルパー関数を呼び出し、通知を使用して通知を表示するjs関数。私は3分ごとにそれを実行しようとすると、それはそのままで動作しています。それは応答しない...いくつかの指導を提供しています....事前に感謝します。ヘルパー関数とjavascript関数を含むdivを自動的に表示する

MY CODE:

コントローラ

<script type="text/javascript"> 
     $(document).ready(function() {setInterval(function() { 
     $('.recentHashMailNotify').load('/application_controller/recentHashMailNotify'); 
    }, 2000); 
}); 
</script> 
をロードするDIV

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

JS関数の部分を含むapplication.html.erbでDIV

コントローラにおけるアクション

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 
+0

notifyHashは何ですか? – trh

+0

新しいメールがあるかどうかをチェックする定義済みのヘルパー関数です。新しいメッセージがある場合、それらのメッセージのサブジュクを文字列@trhの配列として返します。 –

+0

notifyHash関数@trhを追加しました –

答えて

1

もっと良い方法があります。インターバル時間のsetTimeoutを持つjsを作成し、次にajaxリクエストを行い、新しいメッセージを取得する関数を呼び出します。新しいメッセージをjsonとして返し、通知を介してそれぞれのメッセージを処理してから、関数の最後にタイマーをリセットします。この例では、あまりにも速くなる、15秒ごとにリフレッシュしていますし、迷惑:)あなたはconfig/routes.rb

でルートを作成する必要があります

あなたの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); 
    }; 
}); 

のように見えるかもしれませんだろうことに注意してくださいその後

get "/check-messages", to: "users#get_messages" 

あなたはapplication_controller.rbに持っている「ヘルパー」方法、(それはapplication_controllerすると、物事を破ることができるルートを指すようにRailsの風ではありません)users_controller.rbに移動します。

@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はインスタンス変数であり、あなたがあなたのビューで、それが可能な必要がない限り、あなたは(@記号を削除)ローカルに移動する可能性がある

+0

ありがとうございました@trh私はついにそれが正しく動作するようになった!あなたの方法は非常にエレガントなように見えますが、次回は心に留めておきます:) –

関連する問題