Event System (on/off/once)
Use the on(), off(), and once() methods for programmatic event binding.
(function() {
var logEl = document.getElementById('log');
function log(m) { var d = document.createElement('div'); d.className='pkg-log-entry'; d.textContent=m; logEl.appendChild(d); logEl.scrollTop=logEl.scrollHeight; }
var uploader = new MultipleUpload('#demo', {
uploadUrl: '/api/upload',
multiple: true
});
function onComplete(task) { log('taskComplete: ' + task.fileName); }
var listening = true;
uploader.on('taskComplete', onComplete);
uploader.once('init', function() { log('init (once)'); });
document.getElementById('toggle-btn').addEventListener('click', function() {
if (listening) {
uploader.off('taskComplete', onComplete);
log('Listener removed');
} else {
uploader.on('taskComplete', onComplete);
log('Listener added');
}
listening = !listening;
});
})();Two ways to listen
Options like onProgress are set once when the uploader is constructed. on(event, handler) can be added and removed at any time, and several listeners can share one event — which is what you want when different parts of a page each need to react.
The shapes differ slightly
The two APIs carry the same information but not always in the same argument order: on('taskProgress') receives (task, uploader) and you read task.progress, while the onTaskProgress option is handed (task, progress, uploader). Worth checking when converting between them.