简体   繁体   English

Rails资产管道:JavaScript被执行两次

[英]Rails asset pipeline: JavaScript being executed twice

My JavaScript is being executed twice. 我的JavaScript正在执行两次。 When I run the Chrome debugger, I noticed that the individual JavaScript file will execute the code the first time, then after that, the application.js file will execute the code again. 当我运行Chrome调试器时,我注意到单个JavaScript文件将在第一次执行代码,然后, application.js文件将再次执行代码。

I believe this is happening because I recently turned precompile off because I was having issues with Heroku. 我相信这种情况正在发生,因为我最近关闭了预编译,因为我遇到了Heroku的问题。 After this I ran rake assets:precompile . 在此之后我运行了rake assets:precompile

Turbolinks Turbolinks

You probably have an issue with Turbolinks (considering you've included it in your asset pipeline); 可能Turbolinks存在问题(考虑到您已将其包含在资产管道中); although this might not be the problem. 虽然这可能不是问题。

Turbolinks is meant to speed up your page load times by loading the <body> tag of your new pages with ajax , keeping the <head> tag intact. Turbolinks旨在通过使用ajax加载新页面的<body>标记来加快页面加载时间,保持<head>标记完好无损。 This is meant to let your application load just the HTML it needs, but messes up the JS on your page. 这是为了让您的应用程序只加载它需要的HTML,但是在页面上搞乱了JS。

If you're having your JS fired twice, it could be a sign that Turbolinks is interfering with your JS. 如果你的JS被解雇了两次,那么可能表明Turbolinks正在干扰你的JS。 Because you haven't included any examples from the logs , I won't be able to give you any specific information, but you should be able to test this by removing the turbolinks include from both your layout and your application manifest JS file: 因为您没有在logs包含任何示例,所以我将无法向您提供任何具体信息,但您应该能够通过从layoutapplication清单JS文件中删除turbolinks来测试它:

#app/views/layouts/application.html.erb
= javascript_include_tag "application", "data-turbolinks-track" => false

#app/assets/javascripts/application.js
//= require turbolinks -> remove this line

If you do this, restart your server & reload the page in question. 如果这样做,请重新启动服务器并重新加载相关页面。 If it still comes back with the erroneous functionality, it means Turbolinks is not the problem (and you should reset all the things you changed) 如果它仍然带有错误的功能,这意味着Turbolinks不是问题(你应该重置你改变的所有东西)

-- -

Events 活动

The other problem you might have is how you're calling / triggering your events. 可能遇到的另一个问题是您如何调用/触发事件。 This could be down to a number of reasons, but you should definitely look at how you're calling your JS: 这可能有很多原因,但你一定要看看你如何调用你的JS:

#app/assets/javascripts/application.js
$(document).on("click", ".element", function(){
    //your stuff here
});

We always delegate from the document element of the DOM now. 我们现在总是从DOMdocument元素委托。 Whilst this might cause some resource issues, we've found it makes Rails apps much more robust with the Asset Pipeline, as it allows you to truly identify elements on the page. 虽然这可能会导致一些资源问题,但我们发现它使得Rails应用程序在Asset Pipeline中更加强大,因为它允许您真正识别页面上的元素。

If you're trying to trigger an event from a single object on your DOM, you may find that JS will "capture" the event twice (for whatever reason), leading to your issue. 如果您尝试从DOM上的单个对象触发事件,您可能会发现JS将“捕获”事件两次(无论出于何种原因),从而导致您的问题。 Try delegating your events which are being called twice 尝试委派两次被调用的事件

-- -

Functions 职能

Finally, your functions could be the issue. 最后,您的functions可能是问题。

If you have any anonymous functions , or functions for specific events, you will need to ensure they are able to be called correct. 如果您有任何匿名函数或特定事件的函数,则需要确保它们能够被正确调用。 You should do this by using page events in JS & Turbolinks : 您应该通过在JS和Turbolinks中使用页面事件来完成此操作:

#app/assets/javascripts/application.js
var your_function = function() {
    //stuff here
};

$(document).on("page:load ready", your_function);

I had this same problem on a rails 3 app that is not using turbolinks. 我在没有使用turbolinks的rails 3应用程序上遇到了同样的问题。 I was able to fix it by running: 我能够通过运行来修复它:

rake assets:clean

This link helped me: Jquery Rails 3... form submits twice... deletes twice... help 这个链接帮助了我: Jquery Rails 3 ...表单提交两次...删除两次......帮助

I experienced this issue because of the asset pipeline including both jquery, jquery-ujs, and rails-ujs: 我遇到了这个问题,因为资产管道包括jquery,jquery-ujs和rails-ujs:

 //= require jquery //= require jquery_ujs //= require rails_ujs //= require bootstrap //= require turbolinks //= require_tree . 

Removed rails_ujs and all was fine. 删除rails_ujs,一切都很好。

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

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