简体   繁体   English

咖啡脚本在我的ruby应用程序中不起作用

[英]coffee scripts doesn't work in my ruby app

I try to add js actions in my ruby app. 我尝试在我的ruby应用程序中添加js操作。 I read The asset pipeline & Working with JavaScript in Rails guides but I can't reproduce what is described in the second one. 我在Rails指南中阅读了资产管道和使用JavaScript指南,但是无法重现第二篇中描述的内容。 I'm a beginner in rails so perhaps I made too much "manipulations" with files, there extensions... 我是Rails的初学者,所以也许我对文件进行了过多的“操作”,并扩展了...

I have 我有

a controller "mycontroller" app/controllers/mycontroller_controller.rb 控制器“ mycontroller”应用程序/控制器/mycontroller_controller.rb

class mycontrollerController < ApplicationController
    def new
    end
end

application.js app/assets/javascripts application.js app / assets / javascripts

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .

mycontroller.coffee app/assets/javascripts mycontroller.coffee应用程序/资产/ javascripts

When I load the app in a browser, assets seem to be correctly included 当我在浏览器中加载应用程序时,资产似乎已正确包含

<script src="/assets/mycontroller-d915d2e33a0815bf7b1ac3c478a90599.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/application-fdae31866ddfa65ac8bed4781fb60a9c.js?body=1" data-turbolinks-track="true"></script>

Question : do I have to rename .coffee files in .js.coffee? 问题 :我是否必须在.js.coffee中重命名.coffee文件? I tried but nothing change. 我尝试过,但没有任何改变。

I followed "Working with JavaScript in Rails" and modified my files like that: 我遵循“在Rails中使用JavaScript”并按如下方式修改了我的文件:

new.html.erb app/views/mycontroller new.html.erb app / views / mycontroller

<a href="#" onclick="paintIt(this, '#990000')">Paint it red</a>

mycontroller.coffee app/assets/javascripts mycontroller.coffee应用程序/资产/ javascripts

paintIt = (element, backgroundColor, textColor) ->
  element.style.backgroundColor = backgroundColor
  if textColor?
      element.style.color = textColor

I think coffee script is correctly compiled : 我认为咖啡脚本已正确编译:

(function() {
    var paintIt;

        paintIt = function(element, backgroundColor, textColor) {
            element.style.backgroundColor = backgroundColor;
            if (textColor != null) {
                return element.style.color = textColor;
            }
        };

 }).call(this);

But nothing happens by clicking... any idea ? 但是单击...没有任何反应吗?

Thanks in advance for our help. 在此先感谢您的帮助。

The issue is that CoffeeScripts are intended to communicate with the DOM in an "unobtrusive way", so that you don't see any signs of JS in your HTML. 问题在于,CoffeeScripts旨在以“不干扰的方式”与DOM通信,这样您就不会在HTML中看到JS的任何迹象。 And this happens because your script is compiled as a self-executing anonymous function , that isolates its scope from the global namespace. 发生这种情况是因为您的脚本被编译为一个自执行的匿名函数 ,该函数将其范围与全局名称空间隔离。 paintIt doesn't "leak" outside. paintIt不会“泄漏”到外面。 It's by design (and here's some explaination on this ). 这是设计使然(这是对此的一些解释 )。 You need to do that differently. 您需要以不同的方式进行操作。

To distinguish attributes' purpose, I personally place any behaviour-related stuff in data-* attributes and bind events on selectors like [data-paint] , that indicate presence of such attributes. 为了区分属性的目的,我个人将任何与行为相关的内容放置在data-*属性中,并将事件绑定到诸如[data-paint]类的选择器上,以指示此类属性的存在。 I suggest you rewrite your HTML like so: 我建议您这样重写HTML:

<a href="#" data-paint="#990000">Paint it red</a>

Then handle the click event for each data-paint : 然后处理每个data-paintclick事件:

$("[data-paint]").on "click", ->
    # `@` is basically `this` and `@thing` is `this.thing`
    # `@dataset` contains the data attributes of the given element
    @style.color = @dataset.paint

Aa-and you're done. Aa-完成。 See the JSFiddle . 参见JSFiddle

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

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