Custom events with names won't work

I am trying to setup custom named events as described here: http://docs.jquery.com/Namespaced_Events

But I must be missing something because I cannot get events to fire unless the namespace is exactly the same.

I created a fiddle to demonstrate the problem: http://jsfiddle.net/PsR6x/1/

What am I doing wrong?

Update

The second binding is called in jQuery v1.3.2

and v1.5.2

, but not in v1.6.4

and above. The third binding is not called in any version.

on

instead bind

doesn't work either.

+3


source to share


1 answer


Namespaces are not hierarchical.
With the code you have, $('#someid').trigger('griffin.model');

calls everything, and $('#someid').trigger('griffin.updated');

starts everything.

$('body').bind('griffin.model.updated.user', function() {
    alert('Exact namespace = trigger');
});
$('body').bind('griffin.model.updated', function() {
    alert('Will not trigger :(');
});
$('#someid').bind('griffin.model.updated', function() {
    alert('Same item, but not the same namespace = wont trigger');
});

      

Here you have created three separate namespaces for the first and two separate namespaces for the second and third.

Take a close look at this example and read the comments I left for you. Sometimes it can be helpful to try many different things, see different things that you can do.



Relatively on()

not working:
This snippet is taken directly from jquery-1.7.1.js:

bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
}

      

As you can see, it bind()

is just a wrapper for on()

and should work exactly the same, except for bind()

non-supporting selectors or delegation.

+5


source







All Articles