Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: bind namespaces events

Tags:

jquery

Is it possible to listen to ALL events of one basic event if the events are namespaces?

Example:

$elmt.bind("change", function (event) {
    console.log(event);
});
$elmt.trigger("change.namespace1");
$elmt.trigger("change.namespace2");

This only works if I bind to the full event names but this is what I don't know on this position :(

like image 905
mabe.berlin Avatar asked Apr 16 '12 15:04

mabe.berlin


People also ask

What does jQuery use the event namespace for?

The event. namespace property in jQuery is used to return the custom namespace whenever the event is triggered. It is used to handle tasks differently depending on the namespace used.

What is bind event in jQuery?

The bind() is an inbuilt method in jQuery which is used to attach one or more event handlers for selected element and this method specifies a function to run when event occurs. Syntax: $(selector).bind(event, data, function);

What is an event namespace?

Definition and Usage The event. namespace property returns the custom namespace when the event was triggered. This property can be used by plugin authors to handle tasks differently depending on the namespace used.

Which jQuery keyword should be used to bind an event to an element?

bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.


2 Answers

You can, but it's not perfect.

For example:

function eventHandler(event){
    $("#output").html($("#output").html() + "<br />" + event);
}

$elmt = $("#elmt");
$elmt.bind("change.namespace1", eventHandler);
$elmt.bind("change.namespace2", eventHandler);

$elmt.trigger("change.namespace1");
$elmt.trigger("change.namespace2");

JSFiddle is here.

You need to extract the namespace from the event and switch on that though, as both namespaces are fired for the basic "change" event which almost means you need to use "only" namespaces, or no namespaces.

I hope this helps a bit.

like image 127
Andrew Grothe Avatar answered Oct 18 '22 11:10

Andrew Grothe


Cross-posting from Bind to all namespaces of custom jquery event


Try triggerAll instead of trigger:

(function($) {
    $.fn.triggerAll = function(topics, data, delimiter) {
        return this.each(function() {
            var $this = $(this), chain = [], t = '';
            delimiter = (delimiter || '.');
            // rebuild chain
            $.each(topics.split(delimiter), function(i,n) {
                t += (i == 0 ? '' : delimiter) + n;
                chain.push(t);
            });

            // append/prepend original topic?
            data = (data || []);
            data.push(topics);
            $.each(chain, function(i,t) {
                $this.trigger(t, data);
            });
        });
    };
})(jQuery);

Granted, due to how jQuery handles triggering namespacing, triggering the "root" event actually fires the namespaced versions, so to get what you expect you'd need to use another character for the delimiter, like /, and then declare your events like:

var $o = $('#whatever');
// this will be triggered for all events starting with 'root'
$o.on('root', function () { console.log(Array.prototype.slice.call(arguments, 0)); });
// some arbitrary way to fire custom events
$o.on('click', function () {
    $o.triggerAll('root/custom1/subA', ['1st', '2nd'], '/');
    $o.triggerAll('root/custom2', [3, 4, 5], '/');
});
like image 28
drzaus Avatar answered Oct 18 '22 11:10

drzaus