- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我主要是一名后端开发人员,并不擅长与 javascript 相关的东西(以及本地存在的所有框架)。我知道这很糟糕,但事实就是如此。
我对我遇到的问题很着迷,我可能遗漏了一些非常基本的东西。我做了一些研究(Google + 堆栈溢出圣经),但没有发现任何与我遇到的问题类似的案例。我想我只是不知道自己在做什么。让我解释一下。
我正在为一个小型(无用)项目使用 Rails 4,我尝试在 coffeescript 文件中编写一些 javascript“代码”。
显然,我编写的 coffeescript“代码”仅在我重新加载页面时或在 POST
请求之后(例如,当您提交表单时)有效。在 GET
请求中,例如在从一个页面导航到另一个页面期间,没有任何事情发生。
我尝试了一个非常简单的基础 101 基础测试:在页面上显示一条警告消息(你好!)(首先包括一个日期选择器)。
假设我有一个 Article
模型和一个关联的 ArticlesController
来编辑/创建/删除数据。我有以下文件:
app/assets/javascripts/application.js
app/assets/javascripts/articles.js.coffeee
app/controllers/articles_controller.rb
app/models/article.rb
app/views/articles/new.html.erb
app/views/articles/index.html.erb
etc.
我只是在 app/assets/javascripts/articles.js.coffee
中添加一行:
alert "Hello!"
如果我提交表单或重新加载页面,我会看到警告“Hello!”。例如,如果我只是点击一个链接来编辑一篇文章,什么也不会发生。
当我查看两种情况下的 rails 日志时,我有两种不同的场景:
在“它不工作”的情况下,当我执行一个简单的 get
请求(通过单击编辑链接)时,我在日志中有这个:
Started GET "/articles/1/edit" for 127.0.0.1 at 2013-09-17 14:35:28 -0400
Processing by ArticlesController#edit as HTML
Parameters: {"id"=>"1"}
Author Load (0.3ms) SELECT "authors".* FROM "authors" WHERE "authors"."remember_token" = 'ab6fa1c5257585de99459c60f24121c' LIMIT 1
Article Load (0.2ms) SELECT "articles".* FROM "articles" WHERE "articles"."author_id" = ? AND "articles"."id" = 1 ORDER BY published_at DESC LIMIT 1 [["author_id", 1]]
Rendered shared/_error_messages.html.erb (0.1ms)
Rendered articles/edit.html.erb within layouts/application (6.6ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (0.6ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 44ms (Views: 40.4ms | ActiveRecord: 0.4ms)
在“它确实有效”的情况下,例如,当我重新加载相同的编辑页面时,它会重新加载所有内容..
Started GET "/articles/1/edit" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Processing by ArticlesController#edit as HTML
Parameters: {"id"=>"1"}
Author Load (0.3ms) SELECT "authors".* FROM "authors" WHERE "authors"."remember_token" = 'ab6fa1c5257585de99459c60f24121c' LIMIT 1
Article Load (0.1ms) SELECT "articles".* FROM "articles" WHERE "articles"."author_id" = ? AND "articles"."id" = 1 ORDER BY published_at DESC LIMIT 1 [["author_id", 1]]
Rendered shared/_error_messages.html.erb (0.1ms)
Rendered articles/edit.html.erb within layouts/application (5.0ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (0.4ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 26ms (Views: 23.4ms | ActiveRecord: 0.4ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/articles.css?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/articles.js?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2013-09-17 14:37:28 -0400
(i removed some of the lines to make it clearer)
我尝试用
生成 js 文件rake assets:precompile
但它是一样的。
我尝试安装一些 gem,例如 therubyracer、barista,结果是一样的。
我是否遗漏了一些非常基本的东西?我应该读一本关于 javascript 基础的书吗?我应该因为不理解非常简单的事情而被驱逐到阿拉斯加吗?我应该退休还是换工作?
感谢您的帮助。我陷入了抑郁。
最佳答案
这是因为 turbolinks gem。当您单击链接时,浏览器不会真正重新加载页面,它只会执行 AJAX 请求并更改标题和正文,其他一切保持原样。如果你想绕过它,你可以从你的 application.js
文件中删除 turbolinks
或者安装 jquery-turbolinks gem
并把 //= 在需要 jquery 或 jquery ui 之后立即需要 jquery.turbolinks
。
而且我还建议在您的咖啡机中使用 $ ->
或 $(document).ready ->
(在大多数情况下几乎相同)。
关于javascript - rails 4 : coffeescript only works when there is a post request or when i reload the page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18878488/
我是一名优秀的程序员,十分优秀!