jquery-ujs
jquery-ujs copied to clipboard
Disable permanent for new Turbolinks 3
It would be nice to have a disable-permanent option in addition to disable-with option.
disable-permanent would permanently disable a button(ajax:send) until it is replaced by content via Turbolinks 3. If there are any ajax errors then it can handle them accordingly.
Currently when using Turbolinks 3 with the disable-with option the request comes back pretty fast but takes a little more time to fetch the new request to update a section.
The server
def destroy
@product = Product.find(params[:id])
@product.active = false
@product.save!
redirect_to(product_path(@product), change: :product_disable_container)
end
To fix it right now I'm doing something like this. Then handling ajax errors also.
$(document).on('ajax:send', function () {
var $el = $('a[data-disable-permanent]:enabled');
var data = $el.data('disable-permanent');
$el.html(data);
$el.attr('disabled', 'disabled');
});
Thanks for your help.
Thank you for the request. I think this is something that should be handled at turbolinks side. I don't see any usage outside turbolinks case.
@Thibaut WDYT?
@rafaelfranca The use-case makes sense to me but I don't know how we could make that work in Turbolinks. Turbolinks doesn't know about forms or disable-with
. jquery-ujs
is the one handling the click/submit event, making the request, disabling the element, and executing the JS it receives from the server.
One thing we could do in Turbolinks is expose a Turbolinks.loading
flag that we switch to true when we load a new page. Using a hook in ujs, or by monkey-patching $.rails.enableFormElements
, we could then skip enabling the elements when a new page is loading.
I was thinking in providing a hook that turbolinks could use to enable this behavior. My idea is to not couple this project with turbolinks.
What kind of hook do you have in mind?
I completely agree on not coupling the two projects. My idea was to add a hook in ujs, since it's the one initiating the action (doing the first request). Turbolinks only comes into play when Rails returns a Turbolinks.visit
response, at which point the elements have already been disabled by ujs. The hook in question would need to tell ujs to not re-enable the elements, because we're refreshing the page (which other libraries / user JS might want to do too).
Yea, I'm not too sure there needs to be a hook in Turbolinks for the feature. It's basically just disable button permanently. Handle errors accordingly.
:+1: I'll experiment with it. Feel free to send a PR if you want.