在 Rails 3.1 应用程序中使用 RequireJS [英] Using RequireJS with a Rails 3.1 app

查看:47
本文介绍了在 Rails 3.1 应用程序中使用 RequireJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

如果您将 RequireJS 与 Rails 3 (esp 3.1) 应用程序一起使用,它对您来说如何工作?我需要注意任何配置技巧或其他问题吗?

If you've used RequireJS with a Rails 3 (esp 3.1) app, how is that working for you? Any configuration tricks or other gotchas that I need to watch out for?

背景

我正在考虑在 Rails 3.1 中的基于链轮的资产管道上使用 RequireJS,特别是针对 JavaScript 代码.我有两个选择的动机:

I'm contemplating using RequireJS over the Sprockets-based Asset Pipeline in Rails 3.1, specifically for JavaScript code. I have two motivators for this choice:

  • 我想为我的 JS 客户端代码利用 RequireJS 的模块管理.
  • 我想要一个预编译系统,它可以将我的 JS 库代码跟随到其他上下文中.令我惊讶的是,Asset Pipeline 预编译器是 Rails 的内置部分,而不是 Sprockets 本身的一部分.

感谢所有反馈,谢谢!

推荐答案

为了后人,我在这里提出了这个问题:

For posterity, here's where I've come to on this question:

  • RequireJS provides an implementation of the Asynchronous Module Definition API. RequireJS' Why AMD? page lays out the case as to why you'd want to use this.

Sprockets 和 Rails 3 Asset Pipeline 允许对 JavaScript/CoffeeScript 代码进行简单的结构化,但不提供任何真正的模块支持.例如,在 Sprockets 中没有任何命名空间控制.

Sprockets and the Rails 3 Asset Pipeline allow for simple structuring of JavaScript/CoffeeScript code, but don't provide any true module support. For example, there's no namespace control whatsoever in Sprockets.

jQuery(从 1.7 开始)、Underscore、Dojo 和许多其他主要库都实现了 AMD 支持.其他几个主要的 JS 库在近期内似乎有 AMD 支持(例如 Backbone.js).

jQuery (as of 1.7), Underscore, Dojo and numerous other major libraries have implemented AMD support. Several other major JS libraries seem to have AMD support on the near-term horizion (e.g. Backbone.js).

当然可以创建一个集成了 RequireJS 的 Rails 应用程序.为了简化这个过程,我在 github 上创建了 requirejs-rails gem,配置简单通过 r.js 对基于 AMD 的代码进行资产管道感知预编译.当前版本可通过以下方式获得:

It's certainly possible to create a Rails app that integrates RequireJS. To simplify that process, I've created the requirejs-rails gem on github, with straightforward configuration and Asset Pipeline-aware precompilation for AMD-based code via r.js. The current release is available via:

gem install requirejs-rails

这篇关于在 Rails 3.1 应用程序中使用 RequireJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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