8

現在、通知を表示するドロップダウンがあります

<li class="notifications dropdown"> 
  <a class="dropdown-toggle" id="dLabel" role="button" data-remote="true" data-toggle="dropdown" data-target="#" href="/notifications"><i class="icon-user"></i> Notifications</a>
  <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
  </ul>
</li>

ulドロップダウンは空ですが、リンクをクリックすると、レールからのデータをメニューに入力したいと思います。

現在、これは notifications.js.erb にあります

$(".nav li.notifications .dropdown-menu").remove();
$(".nav li.notifications").append("<%= escape_javascript(render('users/notifications', notifications: @notifications)) %>");

通常、ドロップダウン リンクをリモートに設定しますが、ブートストラップのドロップダウンを使用しているため、notifications.js.erb にリクエストが送信されません。そこでコードを手動で呼び出すにはどうすればよいですか?

4

1 に答える 1

6

呼び出しを行う JavaScript コードを追加します。

$(document).ready(function() {
    $('#dLabel').click(function() {
        // Only call notifications when opening the dropdown
        if(!$(this).hasClass('open')) {
           $.ajax({
              type: "GET",
              url: "/notifications",
              async: false,
              dataType: "script"
           });
        }
    });
});

また、非同期にして、ロード中のアイコンをメニューに一時的に配置することもできます...

于 2012-10-14T20:47:59.570 に答える