简体   繁体   English

自定义 javascript function 不适用于带有 Turbolinks 的 rails 5

[英]Custom javascript function not working in rails 5 with Turbolinks

I'm new to javascript and I'm having issues getting a custom function to work in a Rails 5 app.我是 javascript 的新手,在获取自定义 function 以在 Rails 5 应用程序中工作时遇到问题。 I'm trying to add a simple function that sets an active link in a bootstrap navbar heading.我正在尝试添加一个简单的 function 在引导导航栏标题中设置活动链接。 I got everything to work in a codepen but when I add it to my Rails app it's not working.我让所有东西都可以在 codepen 中工作,但是当我将它添加到我的 Rails 应用程序时,它无法正常工作。 I know javascript is running because I'm using bootstrap and those functions work properly.我知道 javascript 正在运行,因为我正在使用引导程序并且这些功能正常工作。 I also think the script is loading by checking the sources in Chrome Developer tools and it is there.我还认为脚本是通过检查 Chrome 开发人员工具中的源来加载的,它就在那里。 However, setting a breakpoint never gets triggered.但是,设置断点永远不会被触发。

I've read through about 30 similar questions and none of them helped fix my issue, so I'm really hoping someone can get me past this roadblock.我已经阅读了大约 30 个类似的问题,但没有一个能帮助解决我的问题,所以我真的希望有人能让我克服这个障碍。

Here is the code I wrote:这是我写的代码:

HTML HTML

<nav class="navbar navbar-light bg-light navbar-expand-md fixed-top header-bg">
  <%= link_to image_tag(asset_path('jungers_farm/jungers_logo.png'),
                  width: '50', height: '50'),
                 root_path, class:'navbar-brand col-4 menu-brand' %>
  <button class="navbar-toggler" type="button" data-toggle="collapse"
          data-target="#navbar-list-2" aria-controls="navbarNav" aria-expanded="false"
          aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbar-list-2">
    <ul class="navbar-nav justify-content-center" id="main-nav-list">
      <li class="nav-item">
        <%= link_to 'Home', root_path, class: 'nav-link active' %>
      </li>
      <li class="nav-item">
        <%= link_to 'Training', page_path('training'), class: 'nav-link' %>
      </li>
      <li class="nav-item">
        <%= link_to 'Breeding', page_path('breeding'), class: 'nav-link' %>
      </li>
    </ul>
  </div>
</nav>

Javascript Javascript

$('.navbar-nav .nav-link').click(function(){
  $('.navbar-nav .nav-link').removeClass('active');
  $(this).addClass('active');
})

Application.js file (my code is in custom.js in app/assets/javascripts) Application.js 文件(我的代码在 app/assets/javascripts 中的 custom.js 中)

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery3
//= require popper
//= require rails-ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require custom
//= require_tree .

Here is the codepen showing that it works in that environment and what I'm trying to accomplish.这是代码笔,显示它在该环境中工作以及我正在尝试完成的工作。

Codepen密码笔

UPDATE So, my initial problem was the code was not loading after the DOM because of turbolinks.更新所以,我最初的问题是由于 turbolinks,代码没有在 DOM 之后加载。 I added the following code and now it is running.我添加了以下代码,现在它正在运行。

$( document ).on('turbolinks:load', function() {
    ... original code ...
})

However, the code is not running as it does in my codepen.但是,代码没有像在我的 codepen 中那样运行。 The active link is not changing as expected.活动链接未按预期更改。 The link that was originally marked as active stays active regardless of the link clicked.无论单击的链接如何,最初标记为活动的链接都将保持活动状态。 Is there some other load issue for the page I'm missing?我缺少的页面是否还有其他加载问题?

So, I discovered that when using Turbolinks in Rails 5 you have to use a Turbolinks event to ensure you trigger your javascript code.因此,我发现在 Rails 5 中使用 Turbolinks 时,您必须使用 Turbolinks 事件来确保触发 javascript 代码。 I regular JQuery $(window) doesn't always do the trick.我经常使用 JQuery $(window) 并不总是有效。 For example you can do the following to trigger your code once after the initial page load, and again after every Turbolinks visit.例如,您可以执行以下操作以在初始页面加载后触发您的代码一次,并在每次 Turbolinks 访问后再次触发。

$( document ).on('turbolinks:load', function() {
  $('.navbar-nav .nav-link').click(function(){
    $('.navbar-nav .nav-link').removeClass('active');
    $(this).addClass('active');
  }) 
})

Turbolinks 5涡轮链接 5

You can read more on the Turbolinks events here: Turbolinks Events .您可以在此处阅读有关 Turbolinks 事件的更多信息: Turbolinks 事件 And more on Turbolinks 5 here: Turbolinks 5 .还有更多关于 Turbolinks 5 的信息: Turbolinks 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM