3

I have worked on two projects using Ajax and Ruby. In one project, I used Rails Prototype helpers and RJS. In the other project I used HAML and unobtrusive Javascript with jQuery. Each had a learning curve which I have overcome. However, the long-term costs and benefits of either approach are not clear. For my next project I am trying to decide between:

  1. Rails + HAML + jRails + jQuery
  2. Rails + HAML + unobtrusive jQuery

The trend in Rails, especially Rails 3.0, seems to be towards using unobtrusive Javascript, but it is not clear to me why this is better, especially when you get so many useful helpers (such as remote_form_for) from Rails/jRails. In trying to make this decision, I have the following questions:

  1. Which approach leads to less code to achieve the same amount of functionality?
  2. Which approach is less error-prone?
  3. Which approach is easier to test?
  4. Which approach is easier to read, maintain, and evolve?
  5. Which approach lets you more easily degrade your web application gracefully (by providing the same functionality without Ajax) when Javascript is disabled in the client browser?

I appreciate any help or insight you can provide.

4

1 回答 1

2

对于你的问题,我有几个答案——至少从我的角度来看是这样。此类问题的答案实际上取决于您的经验和偏好。

1) 我会说不显眼的 JavaScript 将导致整体代码更少,特别是如果您重用 JavaScript 方法。

2 & 3) 我将使用不显眼的 JavaScript 来处理这两个问题。当 JavaScript 与页面结构分离时,您可以更好地了解该特定代码的作用,并且可以对其进行测试,并且知道您对这些方法进行了测试。混合代码通常(根据我的经验)很难获得良好的测试覆盖率。这将有助于保持代码不易出错。

解耦 js 的一个问题是,如果您删除一项功能,您可能会留下代码或删除依赖代码。当然,这就是为什么您应该拥有自动化测试和良好的覆盖率。

4)我认为内联 js 更容易理解,因为它可以在那里但是任何超过一行的东西,并且你已经降低了可读性,所以你不妨一直不显眼以获得其他好处。不显眼的 js 还使其他团队成员更容易维护和记录。

5)根据我的经验,这是一次洗涤。无论哪种方式,您都必须提供额外的方法。

最后,我同意 Rails 3 还为不显眼的 js 提供了更好的支持,我将其视为最佳实践。我刚刚开始为客户修复一个旧应用程序,并且使用 html、js、逻辑的混合视图,要掌握他们想要完成的工作真的很痛苦。如果东西是分开的,那就更容易接受了。

我希望这有帮助。

于 2010-09-13T19:05:03.767 回答