简体   繁体   中英

alertify.js - rails custom confirm dialog

How can I customize my rails confirm dialog with alertify? I tried this code and regarding to the jquery_ujs it should work:

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

example rails call:

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

I am fiddling with this override as well and stumbled on this question. This snippet does not work because the result of alertify.confirm does not get returned to $.rails.confirm .

Update:

After some searching I found a demo by rors .

Important note: In your HTML you will have to have two data-attributes: data-confirm and data-method . Where data-method can be a RESTful method (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);
        }
    })
};

Haml:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM