简体   繁体   English

如何将事件侦听器添加到使用 Jquery Event 构造函数创建的 Jquery Event 中

[英]how do add event listener to Jquery Event , which is created using Jquery Event constructor

I am trying to build a small custom event.我正在尝试构建一个小型自定义事件。 I am very new to jquery and JS in general , have a look at my code below:我对 jquery 和 JS 很陌生,请看下面的代码:

$(function () {

    test = $.Event('click');

    $('a').on( test , function(e){
        console.log(e.target);
        return false;
    })

});

now in the above code when the a is clicked the test event is never fired , the reason being the .on documentation clearly states that the 1st parameter must be a string .现在在上面的代码中,当单击 a 时,永远不会触发test事件,原因是.on文档明确指出第一个参数必须是字符串。

Now my question is suppose I use the event constructor as discussed in this Jquery documentation.现在我的问题是假设我使用了这个Jquery 文档中讨论的event constructor How do I attach an event then to test ?我如何附加一个事件然后进行test

Javascript solution: Javascript 解决方案:

function Dispatcher(){
    "use strict";

    this.listeners = [];

    /* public function dispatch
     * @Description: Execute the callback subscribed to an event.
     * @Arguments: eventName
     * @Return: void;
     */
    this.trigger = function (eventName, eventData) {
        if(this.listeners[eventName]) {
            for(var i=0; i < this.listeners[eventName].length; i++) {
                this.listeners[eventName][i].callback(eventData);
                if(this.listeners[eventName][i].executeOnce) {
                    this.listeners[eventName].splice(i, 1);
                }
            }
        }
    };

    /* public function on()
     * @Description: Subscribe  the callback to certain event
     * @Arguments: eventName, callback, executeOnce.
     * @Return void;
     */
    this.on = function(eventName, callback, executeOnce) {

        var listener = {
                callback: callback,
                executeOnce: executeOnce
        };

        if(!this.listeners[eventName]) { this.listeners[eventName] = []; }
        this.listeners[eventName].push(listener);
    };

    /* public function off()
     * @Description: Un-subscribe all the callbacks subscribed to certain event.
     * @Arguments: eventName
     * @Return void;
     */
    this.off = function(eventName) {
        if(this.listeners[eventName]) {
            delete this.listeners[eventName];
        }
    };

    /* public function one()
     * @Description: Subscribe the callback to be executed only once to the eventName
     * @Arguments: eventName, callback
     * @Return void;
     */
    this.one = function(eventName, callback) {
        this.on(eventName, callback, true);
    };
}

var dispatcher = new Dispatcher();
dispatcher.one('customEvent', function(eventData) {
   console.log('customEventFired');
});
dispatcher.one('customEvent', function(eventData) {
   console.log('another action that depends on this event');
});
dispatcher.trigger('customEvent');
dispatcher.trigger('customEvent');

You can use .type to get the event type from event object.您可以使用.type从事件对象中获取事件类型。 however you can not pass the entire event object in on method as it expects event name and not the event itself:但是您不能在 on 方法中传递整个事件对象,因为它需要事件名称而不是事件本身:

$('a').on(test.type, function(e){
    console.log(e.target);
    return false;
})

Custom events are convenient to use for event triggering and especially for artificial (non-DOM) events (although you can use any name).自定义事件可方便地用于事件触发,尤其是人工(非 DOM)事件(尽管您可以使用任何名称)。

For example:例如:

 var test = $.Event('test'); test.customProperty = 'Something'; var hostObject = { name: 'root' }; $(hostObject).on('test', function(e) { alert(e.customProperty); }) $(hostObject).trigger(test);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM