rails web app页面之间的跳转的时候使用ducument.ready只有在再次加载的时候才生效, 因为rails用了turbolinks,

https://github.com/turbolinks/turbolinks/blob/master/README.md#running-javascript-when-a-page-loads

Running JavaScript When a Page Loads

You may be used to installing JavaScript behavior in response to the window.onloadDOMContentLoaded, or jQuery readyevents. With Turbolinks, these events will fire only in response to the initial page load—not after any subsequent page changes.

In many cases, you can simply adjust your code to listen for the turbolinks:load event, which fires once on the initial page load and again after every Turbolinks visit.

document.addEventListener("turbolinks:load", function() {
// ...
})

When possible, avoid using the turbolinks:load event to add event listeners directly to elements on the page body. Instead, consider using event delegation to register event listeners once on document or window.

  1. $(document).ready 依赖于 DOMContentLoaded 事件
  2. Turbolinks 接管页面后换页不会产生 DOMContentLoaded,所以换页之后 $(document).ready 无效
  3. Turbolinks 提供了 page:change 取代 $(document).ready
  4. 现在第一次加载也会触发 page:change 了,所以 page:change 可以替代 $(document).ready

要注意 turbolinks 会缓存访问过的页面,缓存 restore 的时候也会触发 page:chang,这样的代码在用户后退的时候会重复绑定:

$(document).on 'page:change', ->
$('body').on 'click', ->
console.log('hit')

page:change 和 $().ready 逻辑一样,区别是 turbolinks restore 的时候页面带着之前的状态,而传统页面后退的时候是干净的。

如果要用 turbolinks,最好就用 page:change,并且要写可以反复执行不冲突的代码。

现在用 page:change 很少,换成这样写:

$(document).on 'click', 'body', ->
console.log('hit')

还有个 page:load,考虑到 restore 的问题,page:load 才是对应 $().ready

document.addEventListener("turbolinks:load",
function() {
if ($('#component_component_type').attr("data-value") == "display_item") {
$('.display-item-select').show();
$('.grid-select').hide();
} else if($('#component_component_type').attr("data-value") == "grid") {
$('.grid-select').show();
$('.display-item-select').hide();
} else {
$('.grid-select').hide();
$('.display-item-select').hide();
}
}) $(document).on('change', '#component_component_type_id', function(e) {
if ($('#component_component_type_id').find("option:selected").text() == "display_item") {
$('.display-item-select').show();
$('.grid-select').hide();
} else if($('#component_component_type_id').find("option:selected").text() == "grid") {
$('.grid-select').show();
$('.display-item-select').hide();
} else {
$('.grid-select').hide();
$('.display-item-select').hide();
}
});

 

ducument.ready不生效的问题 ruby on rails的更多相关文章

  1. 10 steps to get Ruby on Rails running on Windows with IIS FastCGI- 摘自网络

    Since the original tech preview release of FastCGI last year, we've been seeing a lot of requests fo ...

  2. ruby on rails在fedora18上install

    ruby on rails 在fedora18下的安装 天朝的网络原因,安装不是很顺畅,所以把过程记录下备用 前面下载rubygem什么的都比较快,新建一个project的时候会出问题 gem new ...

  3. Fedora 16下安装ruby on rails

    Fedora 16下安装ruby on rails 最近在windows下写了些rails小程序,问题一个接一个,到最后终于坚信了那句话“windows不适合用于ruby on rails开发”.于是 ...

  4. 用VirtualBox和vagrant在win7×64上搭建ruby on rails 开发环境

    下载准备 1.vagrant 官方  WINDOWS Universal (32 and 64-bit) http://www.vagrantup.com/downloads.html 2.Virtu ...

  5. How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04

    How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04 链接来自于:https://www.digitalo ...

  6. Linux超快速安装Ruby on Rails

    Linux超快速安装Ruby on Rails 时间 2014-11-25 11:45:11 Flincllck Talk 原文  http://www.flincllck.com/quick-ins ...

  7. 管理不同版本ruby和rails的利器——rvm

    近年来,ruby on rails逐渐火了起来,我想各位码农早就耳闻,特别是那些做B/S项目的童鞋,早就想跃跃一试了. 笔者也是初次接触ruby on rails ,我想,对于初学者来说,最好的学习方 ...

  8. ruby on rails on windows

    这次想系统学会rails,最终目标是将redmine改造成顺手的工具,主要的手段就是开发redmine插件.虽然网上都推荐使用类Unix系统,可手头只有win7系统,就安装了. 难免会遇到这样那样的问 ...

  9. win8平台下Ruby on Rails的第一个web应用

    最近在做一个网站web前端的前期开发,老板要求用Ruby on Rails搭建部署开发环境,上网搜之,发现整个搭建流程比较坑爹,于是用了一款集成软件Bitnami Ruby Stack一键安装到我的w ...

随机推荐

  1. EmguCV 一些基本操作

    http://www.cnblogs.com/alsofly/p/3524866.html?utm_source=tuicool&utm_medium=referral 一.先是在程序中图像的 ...

  2. SSH项目与SSM项目的进入首页的方法

    SSH项目中: jsp页面一般都是存放在WEB-INF下面的目录下,这样我们就不能直接访问到这些jsp页面了,保证了页面的安全性. 在struts的管理中,是利用action来实现页面的跳转,进入in ...

  3. jQuery Ajax(异步请求)

    jQuery异步请求 原始的异步请求是需要创建的 XMLHttpRequest 对象.(IE5,6不支持)目前很多浏览器都支持XMLHttpRequest对象 jQuery ajax常用的回调函数:b ...

  4. Linux下多线程下载工具 - Axel

    Axel 是 Linux 下一个不错的HTTP/FTP高速下载工具.支持多线程下载.断点续传,且可以从多个地址或者从一个地址的多个连接来下载同一个文件.适合网速不给力时多线程下载提高下载速度.比如在国 ...

  5. /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 的区别(转)

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个运 ...

  6. 9-slice-scaling

    9-slice-scaling http://rwillustrator.blogspot.com/2007/04/understanding-9-slice-scaling.html

  7. git命令之git tag 给当前分支打标签

    git tag - 标签相关操作 发表于 2011年06月29日 由 机器猫 标签可以针对某一时间点的版本做标记,常用于版本发布. 列出标签 $ git tag # 在控制台打印出当前仓库的所有标签$ ...

  8. Spring的Bean之Bean的基本概念

    从前面我们知道spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...

  9. iOS项目重命名以及Xcode修改Scheme名称图文详解 (yoowei)

    在iOS开发中,有时候想改一下项目的名字,都会遇到很多麻烦.温馨提醒:记得备份. 看到项目名称,总感觉有点low,所以尝试着要将其更改一下. 项目原结构如下: 更改后的项目结构如下: 下面开始更改操作 ...

  10. yii2 数据库操作(转)

    开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的" Web 应用是 config/web.php),DSN( Data Source Name ...