如何将 CoffeeScript(或 JavaScript)的执行限制为 Rails 3.1 中的特定控制器和操作?
新的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Trevor Burnham在这里很好地回答了这个问题:如何将 CoffeeScript 文件与视图关联?
他说:
如果您对 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:
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
将 CoffeeScript 限制为特定视图的一种方法是为相关 JavaScript 创建一个自定义 sprockets 文件,其格式与 application.js 类似。假设您将其命名为 extras.js。
然后使用 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.
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.
为什么不将特定控制器的 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).
我通常做的是在我的布局中的 javascript 下有一个 Yield:js ,当我需要一个特定的脚本时,我直接从我的视图加载它:
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:
如果您在咖啡脚本中的变量中使用 gon gem,则可以使用这种模式:
为控制器中的每个操作放置一个标志:
在关联的咖啡脚本中,使用这些标志来区分这两个操作:
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:
In the correlating coffee script use those flags to distiguish between the both actions: