事件中的异步调用确认对话框(Asynchronous call confirmation dialog in event)

编程入门 行业动态 更新时间:2024-10-12 10:19:42
事件中的异步调用确认对话框(Asynchronous call confirmation dialog in event)

我想从jQuery确认对话框返回一个布尔值,并将该值返回给一个事件(继续或停止事件的默认执行)。 我知道异步调用,但我真的无法解决这个问题。 这就是我现在所拥有的:

function moveConfirmation() { var defer = $.Deferred(); $('#dialog-move-confirm').dialog({ resizable: false, width: 400, height: 200, autoOpen: true, modal: true, buttons: { 'Move Separately': function() { $(this).dialog('close'); defer.resolve(true); }, 'Move Together': function() { $(this).dialog('close'); defer.resolve(false); }, Cancel: function() { $(this).dialog('close'); defer.resolve(false); } } }); return defer.promise(); } scheduler.attachEvent('onBeforeEventChanged', function(id, ev) { // if move contemporaneous exams, alert user to choose if move together or separately var state = scheduler.getState(); var saveEvent = false; if (state.drag_mode === 'move') { moveConfirmation().then(function(move) { saveEvent = move; }); } return saveEvent; }

发生的事情是saveEvent仍然是假的并且在promise之前返回。 我该怎么办? 我也尝试了另一个承诺..但它仍然回到了同样的事情。 有人看到了解决方法吗?

I want to return a boolean value from a jQuery confirmation dialog and return that value to an event (to either continue or stop default execution of an event). I know about asynchronous calls but I really can't get around this. This is what I have until now:

function moveConfirmation() { var defer = $.Deferred(); $('#dialog-move-confirm').dialog({ resizable: false, width: 400, height: 200, autoOpen: true, modal: true, buttons: { 'Move Separately': function() { $(this).dialog('close'); defer.resolve(true); }, 'Move Together': function() { $(this).dialog('close'); defer.resolve(false); }, Cancel: function() { $(this).dialog('close'); defer.resolve(false); } } }); return defer.promise(); } scheduler.attachEvent('onBeforeEventChanged', function(id, ev) { // if move contemporaneous exams, alert user to choose if move together or separately var state = scheduler.getState(); var saveEvent = false; if (state.drag_mode === 'move') { moveConfirmation().then(function(move) { saveEvent = move; }); } return saveEvent; }

What is happening is that saveEvent is still false and returning before the promise. What should I do? I also tried another promise.. but it still comes back to the same thing. Anyone sees a workaround for this?

最满意答案

您必须知道onBeforeEventChanged中的return语句将始终返回false,因为moveConfirmation().then(...)是异步的。

您可能必须像处理一样取消onBeforeEventChanged事件,而不是简单地设置saveEvent = move; 在moveConfirmation处理程序中,您必须以编程方式重新触发尝试的操作,以防用户想要继续(可能使用updateEvent )。

还请注意,您需要一种机制来避免重新触发过程再次调用对话框(例如,如果在调用updateEvent时触发onBeforeEventChanged )。

编辑:请注意,如果它在UI上看起来更好,也可以接受事件更改,但是如果需要,可以根据moveConfirmation的响应撤消它们。

You must know that the return statement within onBeforeEventChanged will always return false, since moveConfirmation().then(...) is async.

You will probably have to always cancel the onBeforeEventChanged event like you are doing, but rather than simply setting saveEvent = move; within the moveConfirmation handler, you will have to re-trigger the attempted operation programmatically in case the user wanted to proceed (perhaps using updateEvent).

Please also note that you need a mechanism in place to avoid having the re-triggering process to invoke your dialog box again (e.g. if onBeforeEventChanged is fired when calling updateEvent).

EDIT: Note that you can also do the opposite if it looks better on the UI, always accept event changes, but undo them if needed as per the moveConfirmation's response.

更多推荐

本文发布于:2023-08-08 00:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对话框   事件中   Asynchronous   call   dialog

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!