ProgerXP / FileDrop

Self-contained cross-browser pure JavaScript class for Drag & Drop and AJAX (multi) file upload.
filedropjs.org
The Unlicense
264 stars 61 forks source link

Reset FileDrop instance #11

Open rafaelmaiolla opened 10 years ago

rafaelmaiolla commented 10 years ago

Is there a way to reset the FileDrop object instance? Or destroy it to instantiate a new instance?

ProgerXP commented 10 years ago

What do you mean by "reset"? Unbind it from the zone? This is currently not implemented (if you want to remove event listeners simply reinsert the zone node into the DOM) because apart from this I'll have to remove iframe, form, etc. and I don't see much use of this anyway.

rafaelmaiolla commented 10 years ago

I see. I worked around that by removing the elements added by FileDrop and then I was able to instantiate it again in the same zone node.

ProgerXP commented 10 years ago

You supposedly could still instantinate another instance on the same zone node because FileDrop reuses existing elements (it won't create iframe and form twice).

What is the use case of reinstantinating it on the same node in your project?

rafaelmaiolla commented 10 years ago

I tried to re-instantiate it, but it didn't seem to work properly. I didn't investigate why, but the events were not working properly, this could be a problem here for my project.

In my project, when a view is shown, I don't know if the HTML was removed from the DOM and re-created. So, to guarantee that everything will be working, I re-instantiate the FileDrop instance so it can re-attach the events.

ProgerXP commented 10 years ago

It looks like <filedrop>.hook(<zonenode>) would do the trick much better than removing internal stuff like form and iframe. It's also more lightweight and will retain multiple attribute of file input as well as other changes on the DOM tree.

Honestly I didn't test it but I think this should work:

var zone = new FileDrop('id')
...
zone.hook(zone.el)
rafaelmaiolla commented 10 years ago

I think you also need to do the following, or the zone.hook will not find the new elements:

zone.opt.input = null;
ProgerXP commented 10 years ago

This is only needed if you have changed either the <form> or <input type="file">. If you have just reinserted them into the DOM this is optional. In your case it's necessary though.

Nice to know it works.