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.

-DenverCoder10

On Jun 28, 2012 8:13 PM, "James Burke" <jrb...@gmail.com> 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:

requirejs.config({
 shim: {
   'jquery-ujs': ['jquery']
 },
 map: {
   '*': {
     'jquery': 'jquery-adapter'
   },
   'jquery-ujs': {
     'jquery': 'jquery'
   }
});

Then jquery-adapter.js looks like:

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

James

On Thu, Jun 28, 2012 at 3:19 PM, Bill Magnuson <billm...@gmail.com> 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 https://github.com/rails/jquery-ujs). 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.