Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroy #338

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/assets/javascripts/uploadcare/build/_widget.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
expose('globals', uploadcare.settings.common)
expose('start')
expose('initialize')
expose('reset')
expose('fileFrom')
expose('filesFrom')
expose('FileGroup')
Expand Down
8 changes: 7 additions & 1 deletion app/assets/javascripts/uploadcare/widget/base-widget.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,20 @@ uploadcare.namespace 'widget', (ns) ->
dialogApi = uploadcare.openDialog(@currentObject, tab, @settings)
@__onDialogOpen.fire(dialogApi)
return dialogApi.done(@__setObject)

destroy: () ->
@template.content.remove();
@element.data('uploadcareWidget', null).off('.uploadcare')
return

api: ->
if not @__api
@__api = utils.bindAll(this, [
'openDialog'
'reloadInfo'
'value'
'validators'
'validators',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comma is unnecessary

'destroy'
])
@__api.onChange = utils.publicCallbacks(@__onChange)
@__api.onUploadComplete = utils.publicCallbacks(@__onUploadComplete)
Expand Down
6 changes: 6 additions & 0 deletions app/assets/javascripts/uploadcare/widget/live.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ uploadcare.namespace '', (ns) ->
setInterval(ns.initialize, 100)
# should be after settings.common(s) call
ns.initialize()

ns.reset = (container = ':root') ->
$(selector).each () ->
widget = $(this).data(dataAttr)
if(widget)
widget.destroy()

$ ->
if not window["UPLOADCARE_MANUAL_START"]
Expand Down
26 changes: 25 additions & 1 deletion test/dummy/app/views/welcome/with_value.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@
<p>Group incorrect:<br>
<input type="text" data-public-key="1c86ca998ba22e75fbc6" role="uploadcare-uploader" name="GroupIncorrect"
value="cd334b26-c641-4393-bcce-b5041546430d~" data-images-only="" data-multiple="true"/>
<button>Clear</button>
<button>Clear</button></p>


<input type="text" size="30" id="test-memory">
<br>
<button id="test-memory-run">Run memory test!</button>

<script>
UPLOADCARE_CLEARABLE = true;
Expand All @@ -74,4 +78,24 @@
}
});
});

var memoryTestInterval = null;

$('#test-memory-run').on('click', function() {
if (memoryTestInterval) {
clearInterval(memoryTestInterval);
memoryTestInterval = null;
$(this).text('Run memory test!');
return;
}

memoryTestInterval = setInterval(function() {
var N = 100;
for (var i = 0; i < N; i++) {
uploadcare.Widget('#test-memory').destroy();
}
}, 500);
$(this).text('Stop memory test');
});

</script>