简体   繁体   English

Ruby-on-Rails JavaScript未在link_to上加载

[英]Ruby-on-Rails JavaScript not loading on link_to

I'm using Google visualisation to build a charts table in a script tag nested inside a view. 我正在使用Google可视化在嵌套在视图内的脚本标签中构建图表表。 This works well enough when the page is fetched directly but when running through a link_to helper the javascript does not work. 当直接提取页面时,此方法效果很好,但通过link_to helper运行时,javascript无法正常工作。

I believe this was related to turbolinks however wrapping the JavaScript as below with a page:load hasn't made a difference. 我相信这与涡轮链接有关,但是将JavaScript如下包装成page:load并没有改变。 I have confirmed turning off turbolinks does resolve this however the whole app is significantly slower without it. 我已经确认关闭涡轮链接确实可以解决此问题,但是如果没有它,整个应用程序的运行速度将大大降低。

<script  type="text/javascript" charset="UTF-8">
    function resultstable () {
        google.load('visualization', '1', {packages:['table']});
          google.setOnLoadCallback(drawTable);
          function drawTable() {
           var data = new google.visualization.DataTable();
            data.addColumn('string', '');
            data.addColumn('number', 'Lot #');
            data.addColumn('string', 'Client');
            data.addColumn('boolean', 'Commercial');
            data.addColumn('string', 'Site');
            data.addColumn('string', 'Biological Classification');
            data.addColumn('string', 'Actions');
            data.addRows([
                <% @lots.each do |lot| %>
                    [
                    '<%= check_box_tag "lot_ids[]", lot.id %>', 
                    <%= lot.id.to_s %>, 
                    '<%= link_to Client.find(lot.client_id).org.to_s.humanize, client_path(lot.client_id) %>',
                    <%= lot.commercial %>,
                    '<%= lot.site %>',
                    '<%= lot.phylum.to_s + " " + lot.l_class.to_s + " " + lot.genus.to_s + " " + lot.species.to_s %>',
                    '<%= link_to edit_lot_path(lot) do %><i class="icon-edit"></i><% end %> <%= link_to lot_path(lot) do %><i class="icon-time"></i><% end %>'
                    ],
                <% end %>           
            ]);

            function rm_google_classes() {
            var className = 'google-visualization-table-table';
            $('.'+className).removeClass(className);

            var className = 'google-visualization-table-th';
            $('.'+className).removeClass(className);

            var className = 'google-visualization-table-tr-head';
            $('.'+className).removeClass(className);

            var className = 'google-visualization-table-tr-odd';
            $('.'+className).removeClass(className);

            var className = 'google-visualization-table-tr-even';
            $('.'+className).removeClass(className); 

            var className = 'google-visualization-table-td';
            $('.'+className).removeClass(className);  

            var className = 'google-visualization-table-td-number';
            $('.'+className).removeClass(className);  

            var className = 'google-visualization-table-td-bool';
            $('.'+className).removeClass(className);  

            $("#gtable-results table").addClass("table table-condensed"); 
            };

            var table = new google.visualization.Table(document.getElementById('gtable-results'));
              table.draw(data, {allowHtml: true});
              rm_google_classes();

            google.visualization.events.addListener(table , 'sort',
            function(event) {
              rm_google_classes();
            });     
           };
         }

    $(document).ready(resultstable());
    $(document).on('page:load', resultstable());       
</script>

The Visualization API is very particular about how it is loaded, so when you are calling google.load inside another function like that, you have to change your loading structure a bit. 可视化API的加载方式非常特别,因此,当您在类似这样的另一个函数中调用google.load时,必须稍微更改加载结构。 This: 这个:

google.load('visualization', '1', {packages:['table']});
google.setOnLoadCallback(drawTable);

must be changed to this: 必须更改为此:

google.load('visualization', '1', {packages:['table'], callback: drawTable});

Also, your event handlers are not done correctly: 另外,您的事件处理程序未正确完成:

$(document).ready(resultstable());
$(document).on('page:load', resultstable()); 

is calling the resultstable function and passing the return value of the function (null in your case) to the ready event handler, which accomplishes nothing. 正在调用resultstable函数并将该函数的返回值(在您的情况下为null)传递给ready事件处理程序,该处理程序什么都不做。 If you want the resultstable function to be called on document ready, then you need to remove the () from the end of the function name: 如果希望在文档准备就绪时调用resulttable函数,则需要从函数名称末尾删除()

$(document).ready(resultstable);
$(document).on('page:load', resultstable); 

Also, I skimmed over the turbolinks stuff, and it seems like you want to take a different approach here. 另外,我略过了turbolinks的内容,看来您想在这里采用其他方法。 The Google loader dynamically adds a script tag to the <head> of the page, which doesn't get wiped out by turbolinks, so you don't want to keep calling the loader whenever you load a new page. Google加载程序会动态地将脚本标签添加到页面的<head> ,而不会被涡轮链接抹掉,因此您不想在每次加载新页面时都继续调用加载程序。 Try this instead: 尝试以下方法:

function drawTable () {
    // table drawing code
}

function init () {
    $(document).on('page:load', drawTable);
    drawTable();
} 
google.load('visualization', '1', {packages:['table'], callback: init});

That will ensure that the Visualization API is loaded, and the drawTable function is called with every page load. 这将确保加载Visualization API,并在每次加载页面时调用drawTable函数。

One solution may be trying the jquery-turbolinks gem. 一种解决方案可能是尝试使用jquery-turbolinks gem。 Using the jQuery ready() event should work in that case. 在这种情况下,应使用jQuery ready()事件。

See: https://github.com/kossnocorp/jquery.turbolinks 参见: https : //github.com/kossnocorp/jquery.turbolinks

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

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