Rails内嵌JavaScript和最佳实践 [英] Rails inline Javascript and Best Practices

查看:124
本文介绍了Rails内嵌JavaScript和最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Rails的新用户,我正在开发的应用程序进展顺利,但我正在查看生成的HTML,并注意到像...

I'm kinda new to using Rails, and an app I am working on is progressing well - however I'm looking though the generated HTML and noticed things like...

<script type="text/javascript">
//<![CDATA[
Droppables.add(...);
//]]>
</script>

洒在HTML上,这当然与我使用的地方匹配:

Sprinkled around the HTML, which of course matches up with places where I use:

<%= drop_receiving_element ... %>

我想知道的是...有更好的方法来做到这一点,清洁器?这些脚本标签中的一些来自partials,因此将它们放在页面底部在这种情况下不会真正帮助。

What I'm wondering is... is there a better way to do this, or make it cleaner? Some of these script tags are coming from partials so putting them at the 'bottom of the page' doesn't really help in this situation.

另一个选项可能是推送所有这些'标签块'成一个数组,然后写在 application.rhtml 文件,但它仍然有点乱...

Another option may be pushing all these 'tag blocks' into an array then writing them out in the application.rhtml file, but its still a bit messy...

推荐答案

好吧,如果你真的想使用最佳做法...不要使用内联javascript。保持您的HTML,CSS和Javascript清洁和相互隔离。理想情况下,html文件应该可以使用没有CSS和javascript。

Well if you really want to use best practices...Do not use inline javascript. Keep your HTML, CSS and Javascript clean and seperated from eachother. Ideally the html file should be usable without CSS and javascript.

最简单的方法,imo,只是使用纯HTML / CSS构建您的应用程序,并增强与unobtrusive javascript以提供更好的用户体验。

The cleanest way, imo, is to just build your app using plain html/css, and enhance it with unobtrusive javascript to provide a better user experience.

这样做的模式是将所有的JS文件包含在页面底部,并开始运行代码,使用onDomReady 。

A pattern for this is to include all your JS files at the bottom of your page, and start running your code with functionality like onDomReady.

根据评论,我想添加一些可能的选项让你开始:

Based on the comments I would like to add some possible options to get you started:


  • JQuery

  • YUI

  • 原型

这篇关于Rails内嵌JavaScript和最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆