rails/jquery-ujs

Using jquery_ujs in a non-Rails application

TechRsch opened this issue · 1 comments

I posted this on SO here, but have not received any response. It is/was my understanding that this code is really independent of Rails. Is that true? If so, can you advise on my issue?

I have a Ruby / Rack-middleware / Sinatra application and I am trying to use jquery_ujs within it. I have it loaded and available on my page. It does initiate, as breakpoints within the initiation code trigger when it loads. Also, Chrome shows it within the sources. My test has the data-disables-with element specified. But, the related jquery_ujs function never triggers.

I load jquery_ujs, which I have verified, with:

<script type="application/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="application/javascript" src="http://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.js"></script>
<script type="application/javascript" src="assets/jquery_ujs.js"></script>

My code, which does function except for the data-disable-with, is:

<input type="submit" id="loginButton" value="Login" data-disable-with="Processing" data-transition="flip">

The jquery_ujs function that I expect to execute, and which never starts, is:

$document.on('click.rails', rails.linkClickSelector, function(e) {
    var link = $(this), method = link.data('method'), data = link.data('params'), metaClick = e.metaKey || e.ctrlKey;
    if (!rails.allowAction(link)) return rails.stopEverything(e);

    if (!metaClick && link.is(rails.linkDisableSelector)) rails.disableElement(link);

    if (rails.isRemote(link)) {
        if (metaClick && (!method || method === 'GET') && !data) { return true; }

        var handleRemote = rails.handleRemote(link);
        // Response from rails.handleRemote() will either be false or a deferred object promise.
        if (handleRemote === false) {
            rails.enableElement(link);
        } else {
            handleRemote.fail( function() { rails.enableElement(link); } );
        }
        return false;

    } else if (method) {
        rails.handleMethod(link);
        return false;
    }
});

Breakpoints set within this code never fire. Event Listener Mouse Click breakpoints fire for all other listeners, but not this one.

Event Listener Mouse Click breakpoints trigger for jquery.mobile and jquery, so they are there.

I'm going out on a limb to say that it probably has something to do with the scope of functions within the "rails" variable, but I don't see why that would be the case. I am not actually trying to access the internal functions from my code, only asking it to operate on DOM.

Yes, this code should work independent of Rails. The code you are looking for never fired because you are looking in the wrong part of the code. For this case what you want is https://github.com/rails/jquery-ujs/blob/master/src/rails.js#L517.