如何将 CoffeeScript(或 JavaScript)的执行限制为 Rails 3.1 中的特定控制器和操作?

发布于 2024-11-10 00:20:26 字数 203 浏览 0 评论 0原文

新的 Rails 3.1 资源管道非常好,但由于所有 CoffeeScript(或 JavaScript)文件都合并为每个页面中包含的单个文件,因此提出了这个问题:

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

或者我完全以错误的方式处理这个问题?

The new Rails 3.1 asset pipeline is really nice, but since all CoffeeScript (or JavaScript) files get melded down into a single file that is included in every page, it raises this question:

How do I limit the execution of my script to a particular controller or action? Is there a way within my CoffeeScript to know which controller and action was used during the request so that I can put conditional statements in my script?

Or am I approaching this the wrong way altogether?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

被你宠の有点坏 2024-11-17 00:20:26

Trevor Burnham在这里很好地回答了这个问题:如何将 CoffeeScript 文件与视图关联?

他说:

有两种常见的方法:

  1. 使行为以特定元素的存在为条件。为了
    实例,运行注册表单的代码
    应该以类似的内容开头

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

  2. 使行为以 body 元素上的类为条件。你可以
    使用 ERB 设置主体类。这是
    通常需要样式表作为
    出色地。代码类似于

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

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

Trevor Burnham answers this question nicely here: How do I associate a CoffeeScript file with a view?

He says:

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'

And if you're interested in CoffeeScript, Trevor is working on a book that looks to be very good: http://pragprog.com/titles/tbcoffee/coffeescript

最美的太阳 2024-11-17 00:20:26

将 CoffeeScript 限制为特定视图的一种方法是为相关 JavaScript 创建一个自定义 sprockets 文件,其格式与 application.js 类似。假设您将其命名为 extras.js。

//= 需要 my_code.js.coffee

然后使用 javascript_include_tag "extras" 将该代码包含在您想要的视图中,可以通过为这些视图创建自定义布局,也可以使用 content_for()

BTW,您的问题表明,rails 管道强制您将所有 js 资源放入一个文件中。那不是真的。这通常可以有效避免多次往返,但您可以有多个链轮文件。

One way to restrict coffeescript to a particular view is to make a custom sprockets file for the javascript in question, similar in format to application.js. Say you call it extras.js.

//= require my_code.js.coffee

Then use javascript_include_tag "extras" to include that code in the views you want, either by making a custom layout for those views, or by using content_for()

BTW, your question stated that the rails pipeline forces you to put all your js assets in one file. That's not true. That's efficient often to avoid multiple round trips, but you can have multiple sprocket files.

情仇皆在手 2024-11-17 00:20:26

为什么不将特定控制器的 JavaScript 作为该控制器上的视图(因为它们在如此特定的情况下对应于此)?

如果它们足够通用,您可以使用类、id 或数据 (html5) 标记您的视图,并让您的 javascript 查找它们(这样您就可以重用您的代码)。

Why not to put the javascript for the particular controller as a view on this controller (as they correspond there if are so specific)?

If they are general enaugh you can mark your view with classes, ids or data (html5) and make your javascript look for that (so you can reuse your code).

口干舌燥 2024-11-17 00:20:26

我通常做的是在我的布局中的 javascript 下有一个 Yield:js ,当我需要一个特定的脚本时,我直接从我的视图加载它:

content_for :js do
    javascript_include_tag "myscript"
end

what i normally do is to have a yield :js under the javascripts in my layout and when I need a specific script it, I load it directly from my view with:

content_for :js do
    javascript_include_tag "myscript"
end
韬韬不绝 2024-11-17 00:20:26

如果您在咖啡脚本中的变量中使用 gon gem,则可以使用这种模式:

为控制器中的每个操作放置一个标志:

  def index
    @gps_coords = GpsCoord.all

    # Flag for the CoffeeScript to decide which part to run
    gon.index = true;
  end

  def show
    @gps_coord = GpsCoord.find(params[:id])

    gon.lat = @gps_coord.latitude
    gon.lon = @gps_coord.longitude

    gon.show = true;
  end

在关联的咖啡脚本中,使用这些标志来区分这两个操作:

  # index action?
  if gon.index? 
    first_coord = gon.gps_coords[0]
    map.setView([first_coord.latitude, first_coord.longitude], 15);

  # show action?
  if gon.show?
    map.setView([gon.lat, gon.lon], 15);

If you are using the gon gem for your vars in coffee script you can use this pattern:

Put a flag for every action in the controller:

  def index
    @gps_coords = GpsCoord.all

    # Flag for the CoffeeScript to decide which part to run
    gon.index = true;
  end

  def show
    @gps_coord = GpsCoord.find(params[:id])

    gon.lat = @gps_coord.latitude
    gon.lon = @gps_coord.longitude

    gon.show = true;
  end

In the correlating coffee script use those flags to distiguish between the both actions:

  # index action?
  if gon.index? 
    first_coord = gon.gps_coords[0]
    map.setView([first_coord.latitude, first_coord.longitude], 15);

  # show action?
  if gon.show?
    map.setView([gon.lat, gon.lon], 15);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文