You can't change selectors, when loading ujs from gem #263

scooler opened this Issue Jun 1, 2012 · 1 comment


None yet

2 participants

scooler commented Jun 1, 2012

The selectors are on the top of the file in variables available at $.rails but they're used in the way that you can't change them unless you change the script itself (and this is impossible afaik when loading in from gem into asset pipeline).

This sort of shows what mean:

I see two solutions:

  • Refactor events attaching so that it can be easily called later on - with changed selectors. I guess it wouldn't hurt to do it anyway, as I could see cases where you'd want change the selectors "on fly". However this adds another function available for outside world to break :) - expanding api without that much of a need.
  • Change it so that it's possible to initialize $.rails with set of selectors you want, and they'll be used. Thou this would require your custom script in front of jquery-ujs, and would allow you to change it only once (which is what most people would need anyways).

The problem here is that I didn't completely think through the extensibility of the ujs event selectors. I originally refactored the selectors internally to clean up the code and thought that a beneficial side-effect was that you'd be able to extend or modify the selectors without opening up jquery-ujs internally.

Turns out, it doesn't exactly work that way, as things stand. The problem is you can redefine $.rails.linkClickSelector either before or after jquery_ujs.js loads.

If you do it before, then $.rails is not yet defined, which means you have to define the object it yourself first with something like $.rails = {linkClickSelector: 'some-selector'}; But if you do this, then jquery-ujs just ends up overwriting it when it loads on this line.

We could make this work by changing that line to extend the $.rails object if it already exists, but that still wouldn't be ideal, because you'd have to define the entire selector, instead of being able to modify it with linkClickSelector += ', some-selector' or something similar.

If you do it after, which was what I had intended for, it won't work because the delegation events have already been set with the original selectors. Perhaps we could fix this by firing an event after the $.rails object is all set up, but before we attach the event handlers, which would allow you to modify the object within a handler for our new event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment