警报.js - 导轨自定义确认对话框

alertify.js - rails custom confirm dialog

本文关键字:自定义 确认 对话框 js 警报      更新时间:2023-09-26
如何使用

alertify自定义我的轨道确认对话框?我尝试了这段代码,关于它应该工作jquery_ujs:

$.rails.confirm = function(msg){
  alertify.confirm(msg, function (e) {
    if (e) {
        return true;
    } else {
       return false;
    }
  });
};

示例 Rails 调用:

<%= link_to system_communication_gallery_video_path(@gallery.id, video.id), method: :delete, remote: true, confirm: "Are you sure?" do %>

我也在摆弄这个覆盖,并偶然发现了这个问题。此代码段不起作用,因为alertify.confirm的结果不会返回到$.rails.confirm

更新:

经过一番搜索,我找到了rors的演示。

重要提示:在HTML中,您必须有两个数据属性:data-confirmdata-method。其中数据方法可以是 RESTful 方法(GET、POST、PUT、PATCH、DELETE)。

Javascript:

$.rails.allowAction = function(element){
    if( undefined === element.attr('data-confirm') ){
        return true;
    }
    $.rails.showConfirmDialog(element);
    return false;
};
$.rails.confirmed = function(element){
    element.removeAttr('data-confirm');
    element.trigger('click.rails');
};
$.rails.showConfirmDialog = function(element){
    var msg = element.data('confirm');
    alertify.confirm(msg, function(e){
        if(e){
            $.rails.confirmed(element);
        }
    })
};

哈姆尔:

= link_to 'Link title', root_path, {data: {confirm: 'Are you sure you want to go home?', method: 'get'}}