2

I'm using the jQuery File Upload plugin. I initialise the plugin like this:

var _this = this;

_obj = {
  autoUpload: true,
  maxFileSize: 1000000 // 1mb
};

FileProofer.$uploadform.fileupload(_obj)
  .bind('fileuploadprogress', function (e, data) {
    _this.handleFileProgress(data);
  }).bind('fileuploadadded', function (e, data) {
    _this.checkAddedFiles(data);
  })

and then later I detach it with:

FileProofer.$uploadform.fileupload('destroy');

According to the documentation, this should remove all event listeners. However, when I run the same initialisation code again, I can see fileuploadadded is attached double (I use console.log inside _this.checkAddedFiles).

So it seems like the destroy call isn't really working. Am I doing something wrong here?


Some background info for those who are interested: The reason I'm reattaching the fileuploader is that I'm trying to reuse the same html for different products in a shoppingcart.

2
  • After calling the destroy method, have you checked if it is really removing any added event listeners? Commented Jul 7, 2016 at 12:22
  • No, I'm not really aware of a good way to see what event listeners are registered. Commented Jul 7, 2016 at 12:36

1 Answer 1

3

Calling <object>.fileupload('destroy') will only dettach the following events:

    _destroyEventHandlers: function () {
        this._off(this.options.dropZone, 'dragenter dragleave dragover drop');
        this._off(this.options.pasteZone, 'paste');
        this._off(this.options.fileInput, 'change');

according to: https://github.com/blueimp/jQuery-File-Upload/blob/093ec8dfe2eac5e56469ec9e1f6a2af321f4ab0d/js/jquery.fileupload.js#L1308

So, researching a bit more, you'll see that there is a cleanData function that removes the events!

$.cleanData( $('#fileupload') );

Source: https://github.com/blueimp/jQuery-File-Upload/blob/098b507770581d2c8b037fa0a747ff02eb45a329/js/vendor/jquery.ui.widget.js

Hope this could help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.