1

I have implement three jQuery-like functions for controling my custom events (i can't using jquery in my project :-():

var Events = {
   on: function(evt, fn) {
       document.addEventListener(evt, fn);
   },
   off: function() {
       document.removeEventListener(evt, fn);
   },
   trigger: function(evt, params) {
        var event; // The custom event that will be created
            if (document.createEvent) {
                event = document.createEvent("HTMLEvents");
                event.eventName = evt;
                event.initEvent(evt, true, true);
                event.data = params;
                window.dispatchEvent(event);
            } else {
                event = document.createEventObject();
                event.eventName = evt;
                event.eventType = evt;
                event.data = params;
                window.fireEvent("on" + event.eventType, event);
            }
   }
}

When i using my observer implementation i have:

Events.on('custom', function(e) {
       console.log(e.data); // output [5,10,15]
});

Events.trigger('custom', [5, 10, 15]);

But i need modify my trigger function to call listener with additional parameters after event object. Something like jquery.trigger() function with extraParameters attribute. For my example:

Events.on('custom', function(e, a, b, c) {
     console.log(a, b, c); //output [5,10,15]
});

Events.trigger('custom', [5,10,15]);

1 Answer 1

1
    var event = new CustomEvent('build', { 'detail': 'test' });


    // Listen for the event.
    document.addEventListener('build', function (events) {
        console.log(events.detail)
    }, false);

    // Dispatch the event.
    document.dispatchEvent(event);

In this way you can pass data to custom created event

ref

EDIT:

if (window.CustomEvent) {
  var event = new CustomEvent('my-event', {detail: {some: 'data'}});
} else {
  var event = document.createEvent('CustomEvent');
  event.initCustomEvent('my-event', true, true, {some: 'data'});
}

this is jquery equivalent to trigger

Sign up to request clarification or add additional context in comments.

5 Comments

I got the results with event.detail but i need got it with extraParameters as listener function arguments.... It's important for me for one reason: parameters must be passed as array of arguments (not as object)!
@xercool then pass all the data in the detail as an object { 'detail': {'key1': value1, 'key2': 'value2' } }
it doesn't good for me. I need just imitate jquery.trigger() as i wrote in my question. I will have to rewrite a lot of code to change this approach. Sorry
I doubt it is the only way. its compatible with IE 9+
@strikers it is the only way. Now params defined as arguments using extraParameters in all listeners.

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.