Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…han once.
  • Loading branch information...
commit 43bfec1da658145ce0af764256929a8439d1d56f 1 parent 9790178
@JangoSteve JangoSteve authored
Showing with 11 additions and 0 deletions.
  1. +11 −0 src/rails.js
View
11 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 events.click && $.grep(events.click, 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

@sergeykish

Fails with jQuery 1.8, please revert

@JangoSteve
Collaborator

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).

@khustochka

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

@khustochka

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

<script>
  // 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]';
  });
</script>

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

<head>
<script src="http://code.jquery.com/jquery-1.7.2.js" type="text/javascript"></script>
<script src="http://127.0.0.1:3000/src/rails.js" type="text/javascript"></script>
</head>
@JangoSteve
Collaborator

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.
Something went wrong with that request. Please try again.