Reputation: 4898
I've run into some jQuery that has:
.on('mousedown.minicolors', '.minicolors-swatch', function (event) {
-- some code --
})
Control came here directly from jQuery.min.js in response to the mousedown.
The first question is, what is the wrapped set that the on() method is working on? Second, the .minicolors after mousedown seems to be a "namespace," from reading the documentation. "mousedown" is indeed the name of a plugin that has been created here. But what, exactly, does that namespace do for us in mousedown.minicolors?
Thanks for adding any clarity here?
Thanks
Upvotes: 1
Views: 106
Reputation: 4898
Regarding the wrapped set that the .on is being applied to, I can see now that it's $(document). The code is:
//Handle Events
$(document)
.on('mousedown.minicolors', '.minicolors-grid', function (event) {
})
.on('mousedown.minicolors', '.minicolors-space', function (event) {
})
.on('mouseover.minicolors', '.minicolors-grid', function (event) {
})
.on('mouseover.minicolors', '.minicolors-space', function (event) {
});
which is actually
//Handle Events
$(document).on(...).on(...).on(....).on(...);
The punctuation was throwing me, and it still seems like a strange way to define all your event handlers, but I guess it works.
Thanks for the explanation on the namespace.
Upvotes: 0
Reputation: 437336
The first question is, what is the wrapped set that the on() method is working on?
Whatever is before the dot -- your code snippet starts just after that, so we don't know.
This is the delegated version of .on
which sets up one event handler on each element in the wrapped set for .on
(in the delegated version, this is typically just one element since if there were more you could substitute both with any of their common ancestors up to and including document
).
This event handler responds to any event triggered on a descendant that matches the selector provided. For example, $(document).on("click", "div.foo", ...)
installs an event handler on document
that responds to a click on any div having the class foo
.
Second, the .minicolors after mousedown seems to be a "namespace," from reading the documentation. "mousedown" is indeed the name of a plugin that has been created here. But what, exactly, does that namespace do for us in mousedown.minicolors?
The actual plugin name is minicolors
; mousedown
is the (namespaced) event name. What the namespace does is:
plugin1mousedown
and plugin2mousedown
, but those would be two different events. Namespacing allows you to "tag" an event handler while still referring to the same event that a non-namespaced handler refers to.$whatever.off(".minicolors")
. This is very useful whenever a plugin instance is "destroyed".Upvotes: 1
Reputation: 15053
The namespace allows you to easily remove the event handler like this:
.off('mousedown.minicolors');
Otherwise you'd have to remove the eventhandler by also passing the original eventhandler itself:
var originalEventHandler = function() { ... };
el.on('mousedown', originalEventHandler)
el.off('mousedown', originalEventHandler)
Upvotes: 3