简体   繁体   English

每次单击链接以加载新页面时,如何停止加载其他资产?

[英]How do I stop loading additional assets each time I click on a link to load a new page?

I have a weird problem, when I click on a link in my project, I get an extra package of assets, each click = +1 extra package of assets. 我有一个奇怪的问题,当我单击项目中的链接时,会得到一个额外的资产包,每次单击= +1额外的资产包。 10 clicks = 10 copies of the assets. 10次​​点击= 10次资产复制。 (10 images, 10 css files 10 js files instead of 1). (10张图片,10个CSS文件而不是1个10个js文件)。 All my jquery events triggered by 10 times. 我所有的jquery事件触发了10次。 I do not even know where to look for a solution. 我什至不知道在哪里寻找解决方案。 I will be grateful for any help. 我将不胜感激。

my app.js 我的app.js

    //= require jquery
    //= require jquery_ujs

    //= require foundation

    //= require turbolinks
    //= require jquery.easing.1.3.js
    //= require_tree .

    $(function(){
        $(document).foundation();
    });


    $(document).ready(function () {
        var $main_container = $("#main_container");
        var $menu_link = $('#menu-link');
        var $s_menu = $("#s-menu");

        $menu_link.click(function () {
            $main_container.animate({"margin-left": "-200px"}, 100);
            $s_menu.animate({"margin-right": "0"}, 100);
        });

        $(".container-fluid").on("click", function(){
            $main_container.animate({"margin-left": "0"}, 100);
            $s_menu.animate({"margin-right": "-200px"}, 100);
        });


        $('input[type="submit"]').focus(function(){
            $(this).blur();
        });

    });

============ ============

After some experimenting (I removed all js from my project) it seems to me that problem lies in rails caching. 经过一些试验(我从项目中删除了所有js),在我看来问题出在Rails缓存中。 Because each click is forcing browser to download new package of assets. 因为每次点击都迫使浏览器下载新的资产包。

============ ============

I found out that my problem is in turbolinks gem, it caches assets each time and then download new package of assets. 我发现我的问题出在turbolinks gem中,它每次都缓存资产,然后下载新的资产包。 But how I can fix this? 但是我该如何解决呢?

Where are you loading 你在哪里装

<%= javascript_include_tag "application" %>

If you are loading it at the bottom of the page within the body turbolinks refreshes the whole body tag, which could be causing your multiple loading assets when you click a link. 如果要在正文底部的页面底部进行加载,turbolink会刷新整个正文标签,这可能会导致您在单击链接时加载多个资源。 Move it to the top within the head. 将其移至头部顶部。

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

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