【问题标题】:Localhost running jquery twice?本地主机运行 jquery 两次?
【发布时间】:2013-03-08 14:29:55
【问题描述】:

几个月来我一直在开发 Rails 应用程序。今天,当我在 localhost 上运行应用程序时,jquery 似乎在每个页面上执行了两次。

我在 iMac 上运行。

一些症状是:

  1. DataTables 告诉我“无法重新初始化 DataTables”
  2. 完整日历在一个页面上显示 2 个日历,而不是 1 个
  3. 引导下拉菜单不起作用

真的让我很困惑!!!

感谢您的帮助!

更新1

当我将 Cap Deploy 部署到 Rails 虚拟服务器时,相同的应用程序运行良好。所以,在我看来,这与我的 iMac 环境有关。

我不确定要显示什么代码。因为 jquery/javascript 代码似乎在上面的所有数字示例中触发了两次。

这是日历的查看代码(日历现在在页面上显示两次):

<h2><%= current_user.employee.employee_full_name %> - Labor Month</h2>
<%= content_tag "div", id: "calendar", data: {employeeid: current_user.employee.id} do %>
<% end %>

还有咖啡脚本:

$(document).ready ->

$('#calendar').fullCalendar
  ignoreTimezone: true,
  editable: true,
  defaultView: 'month',
  height: 500,
  slotMinutes: 30,
  selectable: true,
  selectHelper: true,

select: (start, end, allDay) ->
  title = $("#title")
  description = $("#description")
  hours = $("#hours")
  workorder = $("#workorder_id")
  actcode = $("#actcode_id")
  $("#dialog-form").dialog
    autoOpen: true
    height: 500
    width: 400
    modal: true
    buttons:
     "Create Labor": ->
       $.create "/events/",
         event:
           workorder_id: workorder.val(),
           actcode_id: actcode.val(),
           title: title.val(),
           description: description.val(),
           hours: hours.val()
           starts_at: "" + start,
           ends_at: "" + end,
           all_day: allDay,
           employee_id: $('#calendar').data('employeeid')
       $('#calendar').fullCalendar('refetchEvents')
       $(this).dialog "close"

      Cancel: ->
        $(this).dialog "close"

eventSources: [{
  url: '/events',
}],

timeFormat: 'h:mm t{ - h:mm t} ',
dragOpacity: "0.5"

eventDrop: (event, dayDelta, minuteDelta, allDay, revertFunc) ->
  updateEvent(event);

eventResize: (event, dayDelta, minuteDelta, revertFunc) ->
  updateEvent(event);

updateEvent = (the_event) ->
  $.update "/events/" + the_event.id,
    event:
      title: the_event.title,
      starts_at: "" + the_event.start,
      ends_at: "" + the_event.end,
      description: the_event.description

更新3

Some gems I'm using related to javascript:
gem 'twitter-bootstrap-rails'
gem 'jquery-rails'
gem 'fullcalendar-rails'
gem 'jquery-rest-rails'
gem 'gmaps4rails'
gem 'bootstrap-editable-rails'
gem 'jquery-datatables-rails'
gem 'jqtree-rails'

更新4

我应该看看 Chrome 检查元素吗?

更新5

@abhi 和 Ogz 向导说要删除公共资产或尝试将其放入 development.rb = config.assets.debug = false 。当我将该行添加到 development.rb 时,错误消失了。

现在我不确定下一步该怎么做。我希望能够调试资产。

【问题讨论】:

  • 几乎不可能不看代码就说什么。
  • 你的脚本是什么样的?你导入了哪些 js 库?
  • 这看起来不像 jquery 执行了两次,而更像是 jquery 数据表插件运行了两次。再一次,没有一些代码很难说什么......
  • 可能性:您有重复的资产,一个在 app/assets 中,一个在 public/
  • 我同意@abhi。 Rails 在预编译资产后两次包含资产,然后尝试在启用资产调试的情况下运行。我只是用rm -rf public/assets 删除了预编译的资产。如果这不适合您,请尝试关闭资产调试。

标签: jquery ruby-on-rails


【解决方案1】:

您最近是否切换到 turbolinks?有一个宝石可以解决这些问题。 (jquery.turbolinks)

如果您在使用 jQuery Turbolinks 后发现某些事件被多次触发,您可能已经将文档事件绑定在 $(function()) 块中。例如,下面这个例子很常见,应该避免:

/* BAD: don't bind 'document' events while inside $()! */
$(function() {
 $(document).on('click', 'button', function() { ... })
});

https://github.com/kossnocorp/jquery.turbolinks#troubleshooting

但您应该考虑通过提供更多详细信息和一些代码来改进您的问题。

【讨论】:

    猜你喜欢
    • 2012-09-14
    • 2023-02-03
    • 2013-07-25
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2016-05-20
    • 1970-01-01
    相关资源
    最近更新 更多