実現したいこと
slim
1= link_to xxxx_path, name: "target_item", data: {confirm: "実行しますか?"}
上記のように確認ダイアログを出し、OKを選択したときだけ、testFunc関数を実行したいです
※coffeeではなくjavascriptで実装したい
問題
answerの値がundefinedになってしまい、testFunc関数が実行されないです
該当のソースコード
jquery
1 $(function () { 2 $("*[name=target_item]").on("confirm:complete", function(event, answer){ 3 if(answer){ 4 testFunc(); 5 } 6 }); 7 }); 8 9 function testFunc(){ 10 ※省略 11 }
間違っている箇所があればご指摘、ご教示お願い致します
実現不可能であれば、その旨の回答頂けると助かります
参考にした情報
2018/11/13 17:45 追記
https://github.com/rails/jquery-ujs/blob/master/src/rails.js
を参照すると
js
1 /* For 'data-confirm' attribute: 2 - Fires `confirm` event 3 - Shows the confirmation dialog 4 - Fires the `confirm:complete` event 5 Returns `true` if no function stops the chain and user chose yes; `false` otherwise. 6 Attaching a handler to the element's `confirm` event that returns a `falsy` value cancels the confirmation dialog. 7 Attaching a handler to the element's `confirm:complete` event that returns a `falsy` value makes this function 8 return false. The `confirm:complete` event is fired whether or not the user answered true or false to the dialog. 9 */ 10 allowAction: function(element) { 11 var message = element.data('confirm'), 12 answer = false, callback; 13 if (!message) { return true; } 14 15 if (rails.fire(element, 'confirm')) { 16 try { 17 answer = rails.confirm(message); 18 } catch (e) { 19 (console.error || console.log).call(console, e.stack || e); 20 } 21 callback = rails.fire(element, 'confirm:complete', [answer]); 22 } 23 //ここでダイアログの選択値とコールバックを使用しているっぽそう 24 //そのため、ダイアログの選択値を持ってくるのは出来なさそうに見える。。。 25 return answer && callback; 26 },
となっており、今現在だとanswerの値を取得するのはできなさそう。。。?
補足情報(FW/ツールのバージョンなど)
・ruby 2.3.3p222 (2016-11-21 revision 56859) [x64-mingw32]
・Rails 5.1.5
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/21 01:55 編集