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. CentOS7+hadoop2.6.4+spark-1.6.1

    环境: CentOS7 hadoop2.6.4已安装两个节点:master.slave1 过程: 把下载的scala.spark压缩包拷贝到/usr/hadoop-2.6.4/thirdparty目录 ...

  2. ASP------ActioinResult之多种返回值

    转载: http://www.cnblogs.com/jiagoushi/archive/2013/01/24/2875454.html http://www.cnblogs.com/lvcha/ar ...

  3. 日志分析 第七章 安装grafana

    grafana依赖mysql存储数据,首先需要安装mysql 安装mysql 解压 # groupadd mysql # useradd -s /sbin/nologin -g mysql mysql ...

  4. springmvc 表单字段list提交问题

    比如用户表user 选课表course 用户表有选课字段list<course> courses=new ArrayList<course>(); <input type ...

  5. hdu 1020 Encoding

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Given a ...

  6. JS通过getBoundingClientRect获取的height可能与css设置的height不一致

    发现如果DOM元素有padding-top或者padding-bottom值时, $(dom).height() = dom.style.display + padding-top + padding ...

  7. ERROR 1130 (HY000) Host ‘hostname’ is not allowed to connect to this MySQL server

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'; FLUSH PRIVILEGES;

  8. cannot start session without errors

    访问phpmyadmin出现如标题错误,解决方案如下: cd /var/lib/php 更改目录权限chown nginx:nginx -R session/ That's All!

  9. Mixing ASP.NET and MVC routing

    Here is the solution I settled on. I installed the NuGet Microsoft.AspNet.FriendlyUrls package. Then ...

  10. Python之迭代器和生成器

    Python 迭代器和生成器 迭代器 Python中的迭代器为类序列对象(sequence-like objects)提供了一个类序列的接口,迭代器不仅可以对序列对象(string.list.tupl ...