用于删除方法的 Rails 自定义 Twitter Bootstrap 模式,回调问题

2023-06-21前端开发问题
3

本文介绍了用于删除方法的 Rails 自定义 Twitter Bootstrap 模式,回调问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

以下代码确实会按预期销毁记录,但回调是从一个模式继承到下一个模式.因此,当一条记录被正确删除时,Rails 也一直在寻找删除以前删除的记录.我正在使用 Twitter Bootstrap 模式窗口,它位于 Rails 视图模板中,并在触发标准 Rails 删除方法时显示,替换了常规的 javascript 对话框.

Following code does destroy records as intended, but the callback is inherited from one modal to the next one. So while a record is properly deleted, Rails keeps looking to delete the formerly deleted ones as well. I'm using a Twitter Bootstrap modal window, that sits in a Rails view template and is shown when a standard Rails delete method is fired, replacing the regular javascript dialog.

回调被触发后如何清除?

How to clear the callback after it has been fired?

$.rails.allowAction = function(element) {

  var message = element.data('confirm'),
  answer = false, callback;
  if (!message) { return true; }

  if ($.rails.fire(element, 'confirm')) {
    myCustomConfirmBox(message, function() {
     callback = $.rails.fire(element,
       'confirm:complete', [answer]);
     if(callback) {
       var oldAllowAction = $.rails.allowAction;
       $.rails.allowAction = function() { return true; };
       element.trigger('click');
       $.rails.allowAction = oldAllowAction;
     }
    });
  }
  return false;
}

function myCustomConfirmBox(message, callback) {
    $('#dialog-confirm').modal('show');
    $('#dialog-confirm button.primary').click(function(){
        callback();
        $('#dialog-confirm').modal('hide');
    });
}

由于我对任何删除操作一遍又一遍地使用相同的基本模式,因此回调排队.所以当一个删除动作之前被取消时,它仍然会在另一个对象的另一个删除实例上被触发,因为回调仍然有效.底线:如何清除回调队列?

edit: Since I'm using the same base modal over and over again for any delete action, the callbacks queue up. So when a delete action has been cancelled before, it will still be triggered on another delete instance of a different object, since the callback is still valid. Bottom line: How to clear the callback queue?

推荐答案

事实证明,由于各种原因,摆弄本机删除方法/回调是一个坏主意.我的解决方法如下.

Turns out it is a bad idea to fiddle with the native delete method/callback for various reasons. My workaround solution is as follows.

在你的视图中有一个删除"按钮,带有一些 JS 数据值:

Have a "delete" button in your view, with some JS data values:

#delete button in view template
link_to "delete", "#", 
   :class => "delete_post", 
   "data-id" => YOUR_POST_ID, 
   "data-controls-modal" => "YOUR_MODAL_LAYER",
       #more bootstrap options here…

Bootstrap 打开模态窗口.在里面,有另一个删除"按钮,设置了远程",所以动作将使用 JS.

Bootstrap opens the modal window. Inside that, have another "delete" button with "remote" set, so the action will use JS.

#delete button in modal window
link_to "delete", post_path(0), 
   :method => :delete, 
   :class => "btn primary closeModal", 
   :remote => true  

CloseModal 是另一个 :class 让我知道何时关闭引导模式窗口.我在 application.js 中为此添加了一个附加函数.注意,默认的path有一个nil值,我们将在下一步通过data-id"参数附加要通过JS删除的真实帖子ID:

CloseModal is another :class for me to know when to close the bootstrap modal window. I've put an additional function for that in my application.js. Note, the default path has a nil value, we'll attach the real post ID to be deleted via JS in the next step via the "data-id" param:

#application.js 
$('a.delete_post').live('click', function(){
    _target = $(this).data('id');
    $('#YOUR_MODAL_LAYER .primary').attr('href', '/posts/' + _target);
});

我们的 Posts 控制器中的 destroy 动作将使用 JS 为已删除的帖子渲染动画:

The destroy action in our Posts controller will use JS to render an animation for the deleted post:

#posts_controller.rb
def destroy
    @post = Post.find(params[:id])
    @post.destroy
    respond_to do |format|
       # format.html { redirect_to(posts_url) }
       format.js { render :content_type => 'text/javascript' }
    end
end

请随意在此处插入效果.在此示例中,我们只是淡出已删除的帖子:

Insert here effects as you please. In this example we are simply fading out the deleted post:

#views/posts/destroy.js    
$("div#post-<%= params[:id] %>").fadeOut();

总的来说,这真的很顺利!

Altogether this works really smoothly!

这篇关于用于删除方法的 Rails 自定义 Twitter Bootstrap 模式,回调问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455