Allow BUTTON elements to use "data-remote" #212

Open
glendel opened this Issue Oct 15, 2011 · 9 comments

Comments

Projects
None yet
6 participants

glendel commented Oct 15, 2011

Hello there, wonderful adapter. Is it possible to add : ", button[data-remote]" to the "rails.js" file ?

// Select elements bound by jquery-ujs
inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote], button[data-remote]',

Thanks

purp commented Dec 20, 2011

+1. Would be very useful. I've got a few custom callbacks since it's not there.

Member

JangoSteve commented Jan 27, 2012

Closing as it's a duplicate of #213 (pull request).

JangoSteve closed this Jan 27, 2012

Member

neerajdotname commented Apr 2, 2012

closing the pull request and opening the issue. Trying to clean up the pull requests.

neerajdotname reopened this Apr 2, 2012

mlooney commented May 25, 2012

any updates on this one?

Member

JangoSteve commented May 25, 2012

#213 still needs a test case, which I haven't had time to write yet.

Member

JangoSteve commented Aug 14, 2012

Closing this and reopening #213, since it has a pull request.

JangoSteve closed this Aug 14, 2012

Member

JangoSteve commented Aug 14, 2012

Reopening this for now, because the pull request (and the code suggested here) is not the correct implementation for the feature being requested.

At first I was going to say this is outside the scope of jquery ujs, because button elements are not meant to behave like links (they don't even support the href attribute), so in order to make this work, we'd have to implement and support functionality to create an ajax request from a custom data- attribute and all that, which I think would be outside the scope of jquery-ujs considering buttons are really meant for you to bind your own javascript functionality anyway.

However, then I started thinking, that because we do support custom ajax queries for select and input elements within forms (e.g. to populate a form after selecting from a dropdown box, which is a common use-case), it would only be consistent to add button support for that same use-case within a form.

The problem is, it's not quite that simple implementation-wise, because we support remote inputs and selects by binding to the change event, whereas providing that support for buttons would require binding to the click event, much the same as the remote links are currently implemented.

But then once that event happens, we'd want to do the same functionality as the remote selects/inputs, with hooking up the custom data-url attribute and all.

TL;DR I think it makes sense from a consistency standpoint to support remote buttons, since we're already supporting remote selects and inputs, but it'd require a bit of refactoring. So, I'm just going to keep this open and tagged as an enhancement for now.

JangoSteve reopened this Aug 14, 2012

Member

JangoSteve commented Feb 19, 2013

For what it's worth, if anyone comes across this, there is a way you can have remote buttons without modifying jquery-ujs.

Include this javascript before including jquery-ujs:

$(document).bind('rails:attachBindings', function() {
  $.rails.inputChangeSelector += ', button[data-remote]';
});

And then, include this in your application js:

$(document).delegate('button[data-remote]', 'click', function(e) {
  $.rails.handleRemote($(this));
  e.preventDefault();
});

bcm commented Feb 19, 2013

not a terrible workaround. along those lines, a small change to how url is calculated in handleMethod would allow that method to be used for buttons as well.

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