Rails turbolinks - window.load not document.load

时间:2014-04-12 09:25:12

标签: javascript jquery ruby-on-rails turbolinks

我在一个.js文件中有这样的代码:

var ready;
ready = function() {
var galleries = $('.car-gallery').adGallery({
    loader_image: '../assets/loader.gif',
    slideshow: {
    enable: false,
    autostart: false,
    speed: 5000,
    start_label: 'Start',
    stop_label: 'Stop',
    // Should the slideshow stop if the user scrolls the thumb list?
    stop_on_scroll: true, 
    // Wrap around the countdown
    countdown_prefix: '(', 
    countdown_sufix: ')',
    onStart: function() {
      // Do something wild when the slideshow starts
    },
    onStop: function() {
      // Do something wild when the slideshow stops
    }
  }});
};
$(document).ready(ready);
$(document).on('page:load', ready);

但是在我的rails 4应用程序中我使用turbolinks,有时是这样,图像没有加载,但文档准备就绪,我无法启动('.car-gallery').adGallery

也许有任何方法不重新加载页面,并使用带有turbolinks的window.load?如何?

1 个答案:

答案 0 :(得分:1)

除了JQueryTurbolinks之外,如评论中所示,似乎你的JQuery没有绑定到页面加载事件,因为它应该

我会推荐这段代码(如果你想保留原生解决方案):

var galleries = function() {
  $('.car-gallery').adGallery({
    loader_image: '../assets/loader.gif',
    slideshow: {
    enable: false,
    autostart: false,
    speed: 5000,
    start_label: 'Start',
    stop_label: 'Stop',
    // Should the slideshow stop if the user scrolls the thumb list?
    stop_on_scroll: true, 
    // Wrap around the countdown
    countdown_prefix: '(', 
    countdown_sufix: ')',
    onStart: function() {
      // Do something wild when the slideshow starts
    },
    onStop: function() {
      // Do something wild when the slideshow stops
    }
  });
};
$(document).on('page:load ready', galleries);

您可以尝试添加此内容:

var alert = function(){
    alert("loaded");
};
$(document).on("page:load ready", alert);

<强>更新

在谈话之后,我们发现这是最有效的解决方案:

#JS
$('.car-gallery').adGallery({
    loader_image: '../assets/loader.gif',
    slideshow: {
    enable: false,
    autostart: false,
    speed: 5000,
    start_label: 'Start',
    stop_label: 'Stop',
    // Should the slideshow stop if the user scrolls the thumb list?
    stop_on_scroll: true, 
    // Wrap around the countdown
    countdown_prefix: '(', 
    countdown_sufix: ')',
    onStart: function() {
      // Do something wild when the slideshow starts
    },
    onStop: function() {
      // Do something wild when the slideshow stops
    }
  });

#view
<%= link_to "link", path, data: { no-turbolink: true } %>