Javascript:取消或让事件继续? [英] Javascript: cancel or let an event continue?

查看:143
本文介绍了Javascript:取消或让事件继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景涉及到Kendo UI,但是我认为它可能通常适用于JavaScript,因此可能适用于JavaScript标签。

My scenario deals with Kendo UI, but I think it probably applies to JavaScript generally, hence the JavaScript tag.

我有一个Kendo调度程序,其中包含编辑事件选项设置为functionA。

I have a Kendo scheduler with the edit event option set to functionA.

在functionA中,我创建一个Kendo窗口(基本上是一个模态),向用户询问一个问题;在一种情况下,编辑事件应该继续并起泡,就好像模态从未在那里,另一方面,它应该阻止默认并返回。

In functionA, I create a Kendo window (basically a modal) that asks the user a question; in one case the edit event should continue and bubble up as if the modal had never been there, in the other, it should prevent default and return.

问题是模态是非阻塞的,所以确认模态出现,但是事件逻辑继续并发生编辑器的内置编辑事件。

The problem is that the modal is non-blocking, so the confirm modal comes up, but the event logic continues and bubbles to the editor's built-in edit event.

如何捕获和暂停当前​​的活动,只有在我的Kendo窗口中得到所需的结果才能继续下去?

How can I capture and pause the current event and only continue it if the I get the desired result out of my Kendo window?

我知道我不能,也不应该使Kendo窗口由于JavaScript的单线程性质阻塞,但是有没有办法将此事件置于暂停状态,如果我这样说,只有恢复它。

I know I can't and shouldn't make the Kendo window blocking due to JavaScript's single threaded nature, but is there a way to put this event on hold, and only resume it if I say so.

基本上,我想做像event.Hold(),如果满足条件,event.Resume(),否则,做event.preventDefault()。

Basically, I want to do something like a event.Hold() and if a condition is met, event.Resume(), otherwise, do event.preventDefault().

推荐答案

更新

我测试了以前发布的代码,我发现它不起作用HT。这是经过测试,按照你想要的方式工作,加上它的一个解决方案:

I tested the code I posted previously and I found that it doesn't work quite right. This is tested and works exactly as you wanted, plus its a drop in solution:

var event_store;

function handle_click(event) {
    event.stopPropagation();
    event_store = event;

    //Open the modal here
    //If it's possible, pass the event pointer to the modal's confirm callback
    //instead of using event_store and pass that pointer to fire_event() when
    //it's confirmed
}

function confirm_handle() {
    resume_event("click");
}

function resume_event(type) {
    if (event_store.target.parentNode) {
        var event;

        if (document.createEvent) {
            event = document.createEvent("HTMLEvents");
            event.initEvent(type, true, true);
        } else {
            event = document.createEventObject();
            event.eventType = type;
        }

        event.eventName = type;

        if (document.createEvent) { //Not IE
            event_store.target.parentNode.dispatchEvent(event);
        } else { //IE
            event_store.target.parentNode.fireEvent("on" + event.eventType, event);
        }
    }
}

strong>

Previous

您可以使用这样的东西来暂停事件冒泡。它将取消事件传播,并在调用点击处理程序时显示模态,并将show_modal变量设置为false。在确认fire_event()被调用并触发原始事件时,此时不显示模态,然后将show_modal重置为true。如果用户没有确认模态,您还应该将show_modal恢复为true。

You could use something like this to "pause" the event bubbling. It cancels the event propagation and shows the modal when the click handler is called and sets the show_modal variable to false. Upon confirming fire_event() is called and it triggers the original event, this time not showing the modal, then resets show_modal back to true. You should also rest show_modal back to true in the event that the user does not confirm the modal.

var show_modal = true;
var event_store;

function handle_click(event) {
    event.stopPropagation();
    event_store = event;

    show_modal = !show_modal;
    if (show_modal) {
        //Open the modal here
        //If it's possible, pass the event pointer to the modal's confirm callback
        //instead of using event_store and pass that pointer to fire_event() when
        //it's confirmed
    }
}

function fire_event() {
    if (document.createEvent) { //Not IE
        element.dispatchEvent(event_store);
    } else { //IE
        element.fireEvent("on" + event_store.eventType, event_store);
    }
}

这篇关于Javascript:取消或让事件继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆