我想从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.
更多推荐
发布评论