Rails 最佳实践在哪里放置不显眼的 javascript
我的 Rails 应用程序(全部为 2.3.5)使用了内联 javascript、rjs、prototype 和 jquery 的总体组合。我们称之为学习或成长的烦恼。最近我越来越迷恋不引人注目的 JavaScript。它使你的 html 变得干净,就像 css 清理它一样。
但我见过的大多数例子都是小例子,他们把所有的 javascript(jquery) 都放在 application.js 中。
现在我有一个相当大的应用程序,我正在考虑构建我的 js 的方法。我喜欢我的脚本仍然靠近视图,所以我在想像
orders.html.erb
orders.js
order.js 包含特定于该视图的不显眼的 javascript 之类的东西。但也许这只是我太保守了:)
我读过 Yehuda Katz 关于这个问题的一些帖子 此处和此处,他解决这个问题的地方。它将遍历您的 js 文件并仅加载与您的视图相关的文件。但可惜我找不到当前的实现。
所以我的问题是:
- 如何最好地构建不引人注目的 JavaScript;管理你的代码,你如何确保从 html 中可以清楚地看出应该做什么。我想好的类名会有很大的帮助:)
- 你如何安排你的文件,将它们全部加载进去?只是几个?您是否在视图中使用
content_for :script
或javascript_include_tag
来加载相关脚本。或者 ... ? - 您是否编写带有参数的非常通用的函数(例如删除)(添加额外的属性?),还是编写非常具体的函数(DRY?)。我知道 Rails 3 中有一个标准集,并且那里的一切都不引人注目。但如何从 Rails 2.3.5 开始呢?
简而言之:在 Rails 中执行不显眼的 javascript 的最佳实践是什么? :)
my rails applications (all 2.3.5) use a total mix of inline javascript, rjs, prototype and jquery. Let's call it learning or growing pains. Lately i have been more and more infatuated with unobtrusive javascript. It makes your html clean, in the same way css cleaned it up.
But most examples i have seen are small examples, and they put all javascript(jquery) inside application.js
Now i have a pretty big application, and i am thinking up ways to structure my js. I like somehow that my script is still close to the view, so i am thinking something like
orders.html.erb
orders.js
where orders.js contains the unobtrusive javascript specific to that view. But maybe that's just me being too conservative :)
I have read some posts by Yehuda Katz about this very problem here and here, where he tackles this problem. It will go through your js-files and only load those relevant to your view. But alas i can't find a current implementation.
So my questions:
- how do you best structure your unobtrusive javascript; manage your code, how do you make sure that it is obvious from the html what something is supposed to do. I guess good class names go a long way :)
- how do you arrange your files, load them all in? just a few? do you use
content_for :script
orjavascript_include_tag
in your view to load the relevant scripts. Or ... ? - do you write very generic functions (like a delete), with parameters (add extra attributes?), or do you write very specific functions (DRY?). I know in Rails 3 there is a standard set, and everything is unobtrusive there. But how to start in Rails 2.3.5?
In short: what are the best practices for doing unobtrusive javascript in rails? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为没有一种最佳实践,但我会让您知道我是做什么的。
我在
public/javascripts/
目录中有一系列 js 文件,每个文件都有自己的用途。一些示例可以是utility.js
chat.js
shopping_basket.js
等等。我使用资产打包器并为我的所有通用功能定义一个大的胖集合,并为我的所有通用功能定义另一个大集合仅限管理员功能。往返服务器的成本太高了。我基本上将所有js包含在第一页加载中,缩小为一个blob(一般来说)
我允许基本的
$(document).ready
钩子内联在页面中,并保持它们非常短。我的 js 文件需要访问的数据与页面内联呈现。 (通常在 DOM 中,有时作为变量 - 例如
var xyz = 100
)我通常会在关闭 javascript 的情况下开发控制器(并确保一切正常),然后将其打开并在需要的地方撒上一些
if request.xhr?
。请记住,Rail 3.1 引入了内置最佳实践,请参阅:http://guides.rubyonrails。 org/asset_pipeline.html - 就我个人而言,我在新管道方面遇到了性能和配置问题,但是许多其他人已经使用它取得了巨大的成功。
I do not think there is one best practice, but I'll let you know what I do.
I have a series of js files each for with their own purpose in the
public/javascripts/
directory. Some examples could beutility.js
chat.js
shopping_basket.js
and so on.I use asset packager and define one big fat collection for all my general use functionality and another for admin only functionality. Round trips to the server cost way too much. I basically include all the js in on first page load minified into one blob (in general)
I allow basic
$(document).ready
hooks inline in the pages, and keep them really short.Data that my js files needs to access is rendered inline with the page. (Usually in the DOM, sometimes as vars - Eg.
var xyz = 100
)I will usually develop my controllers with javascript off (and make sure it all works), then I turn it on and sprinkle a few
if request.xhr?
where needed.Keep in mind, Rail 3.1 introduces a built-in best practice, see: http://guides.rubyonrails.org/asset_pipeline.html - on a personal note I have had performance and configuration issues with the new pipeline, however many others have had great success with it.
我最近记录了我的情况 在 Ruby on Rails 中管理 javascript。我基本上将内容分解为许多小的、粒度的文件,每个文件都有适当的命名空间,然后使用 asset_packager 将它们全部合并到一个文件中以进行生产。
I recently documented how I have been managing javascript in Ruby on Rails. I basically break things down into many small, granular files, each with an appropriate namespace and then merge them all into a single file for production using asset_packager.
我在尝试解决同样的问题时发现了这篇文章,但现有的解决方案没有一个让我觉得是正确的。我写下了我的方法 我喜欢 Rails 的约定优于配置,因此我希望采用相同的方法来包含仅适用于特定操作页面的 Javascript。如果不出意外的话,这至少是增加您选择的另一种方法。
I found this post while trying to solve the same problem, but none of the existing solutions struck me as the right one. I wrote up my approach here. I love Rails' convention over configuration, so I wanted the same approach to including Javascripts that are applicable only to a particular action page. If nothing else, it's at least another approach to add to your options.