gpt4 book ai didi

javascript - 在 Rails 3.1 中如何将 CoffeeScript(或 JavaScript)的执行限制为特定的 Controller 和操作?

转载 作者:数据小太阳 更新时间:2023-10-29 03:51:35 26 4
gpt4 key购买 nike

新的 Rails 3.1 Assets 管道非常好,但由于所有 CoffeeScript(或 JavaScript)文件都被合并到一个包含在每个页面中的文件中,它提出了这个问题:

如何将我的脚本的执行限制到特定的 Controller 或操作?在我的 CoffeeScript 中有没有办法知道在请求期间使用了哪个 Controller 和操作,以便我可以在我的脚本中放置条件语句?

还是我的处理方式完全错误?

最佳答案

Trevor Burnham在这里很好地回答了这个问题:How do I associate a CoffeeScript file with a view?

他说:

There are two common approaches:

  1. Make behavior conditional on the presence of a particular element. For instance, code to run a signup sheet should be prefaced with something like

    if $('#signup').length > 0

  2. Make behavior conditional on a class on the body element. You can set the body class using ERB. This is often desirable for stylesheets as well. The code would be something like

    if $('body').hasClass 'user'

如果您对 CoffeeScript 感兴趣,Trevor 正在写一本看起来非常不错的书:http://pragprog.com/titles/tbcoffee/coffeescript

关于javascript - 在 Rails 3.1 中如何将 CoffeeScript(或 JavaScript)的执行限制为特定的 Controller 和操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6137733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com