11

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 :(

3
  • what exactly are you trying to do? Commented Apr 16, 2012 at 15:41
  • A jquery plug-in triggers namespaced change events (change.channel, change.selected and others). I would like to do an action if one of the change event has been triggered. I don't wont to hard code all possible namespaces. Commented Apr 16, 2012 at 15:47
  • Just came across your question after I posted mine. I would also like to know if this is possible. Commented Sep 25, 2012 at 20:04

3 Answers 3

1

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.

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

Comments

1

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], '/');
});

Comments

0

This is what I have used to compare existing theme on each li element and replace with new custom theme.....

$('li').each(function(index) {
                  var oT = $(this).attr('data-theme');
                  var li_item = $(this).attr(index);

                    $('#li_item ').mousedown(function() {

                        if(oT=='a')
                         {
                           $(this).removeClass('ui-btn-up-' + a').addClass('ui-btn-up-' + 'c').attr('data-theme', 'c');
                         }  

                    });

                    $('#li_item ').mouseup(function() {


                        if(oT=='c')
                         {
                           $(this).removeClass('ui-btn-up-' + 'c').addClass('ui-btn-up-' + 'a').attr('data-theme', 'a');
                         }   

                    });

});

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.