bootstrap modal delegate eventype syntax

Go To StackoverFlow.com

2

var Modal = function (content, options) {
   this.options = options
   this.$element = $(content)
   .delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this))
                                          ^^^^^^^^^^^^^^^^^^^^
                                        What does this mean?
}

I am quite fresh. I haven't seen this sort of syntax before. My guess is click event, on any attribute which contains a 'dismiss' and have value of 'modal'???

2012-04-04 19:33
by River


5

I think, what you are seeing is Namespaced Events. This is used extensively when you have multiple event handlers for a same event. See example below,

DEMO

$('#test').on('click.a', function () {
    alert('A is clicked');
});

$('#test').on('click.b', function () {
    alert('B is clicked');
});

$('#unbindA').on('click', function () {
    $('#test').off('click.a');
});

In the above example it just demonstrates unbind but it can also be used for triggering a specific handler like $('#test').trigger('click.a').

All the above is not possible when you just bind 'click' to both the handlers.

jQuery docs on Namespaced Events

2012-04-04 19:39
by Selvakumar Arumugam
+1 for the link to jsfiddl - Kelvin 2012-05-22 17:44
So in .on('click.a' the .a part is a name you can just make up, correct? It doesn't refer to anything in the framework, but is just a way to uniquely name your handler so you can reference it later in trigger/off/etc. - AaronLS 2015-03-11 20:33
@AaronLS Yes, that is correct. You can read about it http://api.jquery.com/on/#event-name - Selvakumar Arumugam 2015-03-11 22:33


0

To add to Vega's answer:

Calling on() (available since jQuery 1.7) or delegate() with the event click.dismiss.modal means to register a click handler that is fired when the dismiss or the modal namespaces are passed in the triggered event, or if click is called with no namespace.

The modal is not "nested" under dismiss (the syntax may be misleading), but they are treated as directly "attached" to click. That means click.dismiss.modal is equivalent to click.modal.dismiss.

Example:

$('#elem').on('click.dismiss.modal', function () {});
$('#elem').trigger('click.dismiss');  // handler is called
$('#elem').trigger('click.modal');  // handler is called
$('#elem').trigger('click');  // handler is called
$('#elem').trigger('click.abc');  // handler is NOT called

Note that trigger(), unbind(), and off() only accept one namespace.

$('#elem').off('click.dismiss');  // now the handler is just 'click.modal'

You can also unbind using .dismiss which will remove all events with that namespace (not just click). Triggering won't work with a namespace by itself; there must be an event name before the dot.

2012-05-22 16:57
by Kelvin
Ads