Google Groups

Re: [requirejs] Re: Requirejs with jQuery UJS

Bill Magnuson Jun 28, 2012 5:49 PM
Posted in group: requirejs

Thanks James, this will work great.


On Jun 28, 2012 8:13 PM, "James Burke" <> wrote:
With requirejs 2.0:

Since jquery-ujs does not call AMD's define, it needs a shim, then use
the 'map' config to instruct the loader to load the jquery-adapter for
anyone who asks for jquery. This assumes all your modules ask for
'jquery' as their dependency:

 shim: {
   'jquery-ujs': ['jquery']
 map: {
   '*': {
     'jquery': 'jquery-adapter'
   'jquery-ujs': {
     'jquery': 'jquery'

Then jquery-adapter.js looks like:

define(['jquery', 'jquery-ujs'], function ($) {
 return $;


On Thu, Jun 28, 2012 at 3:19 PM, Bill Magnuson <> wrote:
> Like OP, I'm trying to use require.js with Rails as well. For those without specific experience, in order for jQuery $.ajax calls to function properly, you need to load up jquery-ujs (hosted at Among other things, it works with some rails helpers in the HTML to provide cross site request forgery protection headers in all AJAX calls. Without these, the server will reject ajax calls.
> In my mind, my modules aren't necessarily dependent on jquery-ujs, they just happen to be hitting a Rails server, so without the patches in jquery-ujs, they won't work.
> I'd love for my code to be able to just say it is dependent on jquery and have successful loading of jquery include the loading of jquery-ujs. How would people suggest I do that? I'm already shimming jquery-ujs loading to be dependent on jquery, and I could just add jquery-ujs as a dependency in every module that makes ajax calls, but that seems unclean.