cross-posting da Bind to all namespaces of custom jquery event
Prova triggerAll
invece di 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);
scontato, a causa di come jQuery gestisce innescando namespacing, innescando l'evento "root" spara in realtà le versioni namespace , quindi per ottenere ciò che ti aspetti, devi utilizzare un altro carattere per il delimitatore, ad esempio /
, quindi dichiarare i tuoi eventi come:
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], '/');
});
cosa stai cercando di fare esattamente? – Evan
Un plug-in jquery attiva eventi di modifica con nome (change.channel, change.selected e altri). Mi piacerebbe fare un'azione se uno degli eventi di cambiamento è stato attivato. Non voglio codificare tutti i possibili spazi dei nomi. –
Ho appena trovato la tua domanda dopo aver postato [il mio] (http://stackoverflow.com/questions/12590231/bind-to-all-names-details-of-custom-jquery-event). Vorrei anche sapere se questo è possibile. – jschr