2

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:

$(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 .

Now my question is suppose I use the event constructor as discussed in this Jquery documentation. How do I attach an event then to test?

3
  • From a curiosity point of view, if you know the event is going to be click, why dont you just feed click into the jQuery? $("a").on("click", function(){}); Commented May 27, 2015 at 12:41
  • @Fallenreaper , i can't :( i am trying to create a functinality with custom events only ! Commented May 27, 2015 at 12:49
  • ahh, making a new custom Event Object and feeding this new event into jQuery for manipulation. So in theory, you would have an event called: "Foo" Commented May 27, 2015 at 13:05

3 Answers 3

2

Javascript solution:

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');
Sign up to request clarification or add additional context in comments.

2 Comments

This is what i don't want , but thanks for trying haha , not going pure JS any sooner LOL
No problem, Know that the OP is about jquery, but posted this to add a different way to do it without depending on a library. its just another example :)
1

You can use .type to get the event type from event object. however you can not pass the entire event object in on method as it expects event name and not the event itself:

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

2 Comments

yes , but i need to use a custom event ! .. not a normal Jquery event , i am trying to create a demo where in i want to test if preventDefault() and DefaultPrevented() work with custom events , and thats why , your solution is a no-go for me !
You can not pass the event handler to on method. You can get the type from event however if required.
1

Custom events are convenient to use for event triggering and especially for artificial (non-DOM) events (although you can use any name).

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.