Browse files

Detect and throw error when jquery-ujs is included in document more t…

…han once.
  • Loading branch information...
1 parent 9790178 commit 43bfec1da658145ce0af764256929a8439d1d56f @JangoSteve JangoSteve committed Aug 13, 2012
Showing with 11 additions and 0 deletions.
  1. +11 −0 src/rails.js
@@ -45,6 +45,17 @@
* });
+ // Cut down on the number if issues from people inadvertently including jquery_ujs twice
+ // by detecting and raising an error when it happens.
+ var alreadyInitialized = function() {
+ var events = $(document).data('events');
+ return && $.grep(, function(e) { return e.namespace === 'rails'; }).length;
+ }
+ if ( alreadyInitialized() ) {
+ $.error('jquery-ujs has already been loaded!');
+ }
// Shorthand to make it a little easier to call public rails functions from within rails.js
var rails;

5 comments on commit 43bfec1

Fails with jQuery 1.8, please revert


JangoSteve replied Aug 14, 2012

No need to revert, I haven't added jQuery 1.8 support yet. But this will get fixed when I do (which will be shortly).

For me it fails on JQuery 1.7.2 (which is supported) as well:

Uncaught TypeError: Cannot read property 'click' of undefined :3000/assets/jquery_ujs.js?body=1:52

This failure does not show up in tests because of this snippet:

  // This is for test in override.js.
  // Must go after jQuery is loaded, but before jquery-ujs.
  $(document).bind('rails:attachBindings', function() {
    $.rails.linkClickSelector += ', a[data-custom-remote-link]';

If this is removed an above mentioned failure happens (Uncaught TypeError: Cannot read property 'click' of undefined). Could be reproduced on a bare page:

<script src="" type="text/javascript"></script>
<script src="" type="text/javascript"></script>

JangoSteve replied Aug 15, 2012

Ah, good catch. Fixed in 2f6447c. I think that should also make things better for jquery 1.8 (the feature won't work, but it shouldn't cause an error either (and I'll make the feature work again when I get to actual 1.8 support)).

Please sign in to comment.