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'???
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,
$('#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
.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
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.