Sunday, 15 January 2012

ruby on rails - Can javascript listen for the result of a confirm dialogue from a separate method? -



ruby on rails - Can javascript listen for the result of a confirm dialogue from a separate method? -

i know usual js way of taking action if confirmation dialog have confirmation within function, jquery example:

<script type="text/javascript"> $(function () { $("#dropdown").change(function () { var success = confirm('are sure want alter dropdown ????'); if (success == true) { // } else { // cancel alter event , maintain selected element } }); }); </script>

this doesn't work in case, because confirmation part of rails helper:

<%= link_to "delete", {:controller => 'foo', :action => 'delete', :id => @item.id}, {:method => :post, :confirm => "are sure?", :class => "start_spinner"} %>

i want run javascript on click (hide link prevent clicking twice), if confirmation true. unfortunately, @ to the lowest degree in rails 2.3.x, it's not possible utilize :onclick when :method or :confirm options present. (this behavior noted on apidock link_to helper , same in application.)

edit: think created confusion including illustration apidock instead of exact code. sorry that. i've modified code below reflect behavior rails has when :method => :post alternative included, must in code. difference between 2 outputs still dropping destroyjsfunction method, there's whole bunch of other stuff happen in onclick seems interfere implementing solutions suggested sukima , agnoster.

<%= link_to "delete", {:controller => 'foo', :action => 'delete', :id => @item.id}, :method => :post, :confirm=>"are sure?", :onclick=>"destroyjsfunction()", :class => "start_spinner" %> # expected output # => <a href="/foo/delete/1" class='start_spinner' onclick="if (confirm('are sure?')) { var f = document.createelement('form'); f.style.display = 'none'; this.parentnode.appendchild(f); f.method = 'post'; f.action = this.href;var s = document.createelement('input'); s.setattribute('type', 'hidden'); s.setattribute('name', 'authenticity_token'); s.setattribute('value', 'csrf_token'); f.appendchild(s);f.submit(); }; {destroyjsfunction()}; homecoming false;">delete</a> # actual output # => <a href="/foo/delete/1" class='start_spinner' onclick="if (confirm('are sure?')) { var f = document.createelement('form'); f.style.display = 'none'; this.parentnode.appendchild(f); f.method = 'post'; f.action = this.href;var s = document.createelement('input'); s.setattribute('type', 'hidden'); s.setattribute('name', 'authenticity_token'); s.setattribute('value', 'csrf_token'); f.appendchild(s);f.submit(); }; homecoming false;">delete</a>

end edit

because i'm using rails submit post request, can't re-write link in plain html.

i'd run javascript on click of link class start_spinner, can't find way re-display link if confirmation false. there way have separate javascript hear results of confirmation dialogue?

all confirm place javascript in html describe: prompt user , submit form on success response.

if want different have write own confirm method. since logic simple i'm sure rails team felt if wanted more write yourself.

make function confirm , if/else logic. i'd utilize own unobtrusive js using jquery attach click event utilize rails link_to helper alternative :onclick mentioned above (i detest option.)

jquery example:

class="snippet-code-js lang-js prettyprint-override">$(function() { $('a.destroy').on('click', function(e) { e.preventdefault(); var $el = $(this); var response = confirm($el.data('confirm') || 'are sure?'); if (!response) { return; } $el.css({'pointer-events': 'none'}); $.ajax({ url: $el.attr('href'), type: 'delete', data: {_method: 'delete'} }) .then(function() { alert('deleted! should here'); }) .fail(function() { alert('opps, didn\'t delete, check respose see why.'); }) .always(function() { $el.css({'pointer-events': 'auto'}); }); }); // illustration not needed real code $('#reset').on('click', function(e) { e.preventdefault(); $('a').css({'pointer-events': 'auto'}); }); }); class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#/path/with/:id" class="destroy" data-confirm="are sure?">confirm?</a> (<a href="#" id="reset" title="this not need in real code">reset</a>)

javascript ruby-on-rails

No comments:

Post a Comment