Rails:使用ajax填充bootstrap下拉列表

Mat*_*Hui 7 javascript ajax ruby-on-rails ruby-on-rails-3 twitter-bootstrap

我目前有一个显示通知的下拉列表

<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>
Run Code Online (Sandbox Code Playgroud)

ul下拉列表为空,但点击链接后我想用rails中的数据填充菜单.

我目前在notifications.js.erb中有这个

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

通常我会将下拉链接设置为远程,但由于我使用的是bootstrap的下拉列表,因此没有请求发送到notifications.js.erb.如何在那里手动调用代码?

mcc*_*nnf 5

我会添加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"
           });
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

您也可以使它异步并暂时将加载图标放入菜单中...