【问题标题】:JQuery Sortable - disable "update" event from within the "receive" event?JQuery Sortable - 从“接收”事件中禁用“更新”事件?
【发布时间】:2012-05-23 07:28:11
【问题描述】:

我正在使用可排序的有序列表并根据以下内容对数据库进行更新:

  1. 将新项目拖到列表中,调用“receive”事件进行数据库插入
  2. 对列表进行排序,更新数据库中的显示顺序。

现在,由于“更新”和“接收”事件都是可排序的一部分,并且每个都设置为执行 ajax 发布(异步),我无法控制这些事件的顺序。我需要在“更新”事件之前触发并完成“接收”事件。

我的想法是从接收事件中取消绑定更新事件,然后重新绑定或手动调用更新事件,但我无法让它工作。

我是 jquery 的新手,非常感谢任何帮助!!!

提前致谢, 乍得

    $("#myMovieListItems").sortable({
            receive: OnReceive,
            update: OnSortableUpdate,
            placeholder: 'ui-state-highlight',
            containment: 'document',
            revert: true
        });
function OnReceive(event, ui) {
            //Disable the update event and manually call it on success of this event
            $("#myMovieListItems").unbind("update");

            //Set ID of new list element with it's original value
            var newItem = $(this).data().sortable.currentItem;
            newItem.attr('id', $(ui.sender).attr('id'));

            //Get Data to pass to AJAX method
            droppedID = newItem.attr('id');
            droppedName = $.trim(newItem.first().contents().filter(function () {
                return this.nodeType == 3;
            }).text());

            messageContainer.html(progressMessage);
            source = "Facebook";

            $.ajax({
                type: 'POST',
                url: GetSiteRoot() + 'DesktopModules/Incite/MovieRotation/Handlers/Sortable.asmx/InsertMovie',
                data: '{strMovieName: \'' + droppedName + '\', intMovieListID: \'' + movieListID + '\', strSource: \'' + source + '\', strSourceID: \'' + droppedID + '\'}',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: OnAddMovieSuccess,
                error: OnSortableUpdateError
            });
        }

【问题讨论】:

    标签: jquery jquery-ui


    【解决方案1】:

    我只是在 sortable 上遇到了同样的问题,最终使用了来自 underscore.js 的 debounce

    http://underscorejs.org/#debounce

    它可以将事件处理程序包装在计时器中,例如您可能触发了多个事件但只想调用一次处理程序。对于可排序,我在接收和更新时触发一个自定义事件,然后有一个去抖动处理程序来监听它,并且随着时间的推移,确保它只在收到最后一个事件时被调用一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2016-09-25
      • 2011-12-30
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      相关资源
      最近更新 更多