I am using custom events to broadcast navigation messages (between related plugins).
The simple trigger code currently looks like this:
$button.trigger('navigate', { url: link, target: target });
The receiving code looks like:
$element.on('navigate', function (e, params)
{
e.preventDefault();
e.stopPropagation();
// Do something custom here with params.url and params.target
});
The problem is I also need to test isDefaultPrevented() on the event object that was sent to trigger.
I can create a custom event object and test it with:
var eo = $.Event('navigate');
$button.trigger(eo);
if (!eo.isDefaultPrevented())
{
// Do the default actions
}
But jQuery's trigger() does not have a version that takes both a JQueryEventObject and parameters and I can't figure out how to pass the parameters through with the trigger.
Note: I do not want to use the e.data property as loads of existing code expects the params object to be provided as the second parameter to any event handlers. I would prefer something that worked like this:
var eo = $.Event('navigate');
$button.trigger(eo, { url: link, target: target });
Q: How do you use a custom JQueryEventObject and pass parameters to trigger?
Note: I am using TypeScript, so I am going by what the jQuery definitions file says is available.