Simplify code to delete a cached attachment
We were already defining the links with data-remote and data-method, so instead of manually doing an AJAX request we can rely on Rails to perform the request and then handle the `ajax:complete` event.
This commit is contained in:
@@ -17,11 +17,11 @@
|
|||||||
App.Documentable.lockUploads();
|
App.Documentable.lockUploads();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
App.Documentable.initializeRemoveCachedDocumentLinks();
|
||||||
},
|
},
|
||||||
initializeDirectUploadInput: function(input) {
|
initializeDirectUploadInput: function(input) {
|
||||||
var inputData;
|
var inputData;
|
||||||
inputData = this.buildData([], input);
|
inputData = this.buildData([], input);
|
||||||
this.initializeRemoveCachedDocumentLink(input, inputData);
|
|
||||||
$(input).fileupload({
|
$(input).fileupload({
|
||||||
paramName: "attachment",
|
paramName: "attachment",
|
||||||
formData: null,
|
formData: null,
|
||||||
@@ -57,11 +57,6 @@
|
|||||||
$(data.addAttachmentLabel).hide();
|
$(data.addAttachmentLabel).hide();
|
||||||
destroyAttachmentLink = $(data.result.destroy_link);
|
destroyAttachmentLink = $(data.result.destroy_link);
|
||||||
$(data.destroyAttachmentLinkContainer).html(destroyAttachmentLink);
|
$(data.destroyAttachmentLinkContainer).html(destroyAttachmentLink);
|
||||||
$(destroyAttachmentLink).on("click", function(event) {
|
|
||||||
event.preventDefault();
|
|
||||||
event.stopPropagation();
|
|
||||||
App.Documentable.doDeleteCachedAttachmentRequest(this.href, data);
|
|
||||||
});
|
|
||||||
if (input.lockUpload) {
|
if (input.lockUpload) {
|
||||||
App.Documentable.showNotice();
|
App.Documentable.showNotice();
|
||||||
}
|
}
|
||||||
@@ -76,7 +71,6 @@
|
|||||||
buildData: function(data, input) {
|
buildData: function(data, input) {
|
||||||
var wrapper;
|
var wrapper;
|
||||||
wrapper = $(input).closest(".direct-upload");
|
wrapper = $(input).closest(".direct-upload");
|
||||||
data.wrapper = wrapper;
|
|
||||||
data.progressBar = $(wrapper).find(".progress-bar-placeholder");
|
data.progressBar = $(wrapper).find(".progress-bar-placeholder");
|
||||||
data.errorContainer = $(wrapper).find(".attachment-errors");
|
data.errorContainer = $(wrapper).find(".attachment-errors");
|
||||||
data.fileNameContainer = $(wrapper).find("p.file-name");
|
data.fileNameContainer = $(wrapper).find("p.file-name");
|
||||||
@@ -122,28 +116,10 @@
|
|||||||
showNotice: function() {
|
showNotice: function() {
|
||||||
$("#max-documents-notice").removeClass("hide");
|
$("#max-documents-notice").removeClass("hide");
|
||||||
},
|
},
|
||||||
doDeleteCachedAttachmentRequest: function(url, data) {
|
initializeRemoveCachedDocumentLinks: function() {
|
||||||
$.ajax({
|
$("#nested-documents").on("ajax:complete", "a.remove-cached-attachment", function() {
|
||||||
type: "POST",
|
|
||||||
url: url,
|
|
||||||
dataType: "json",
|
|
||||||
data: {
|
|
||||||
"_method": "delete"
|
|
||||||
},
|
|
||||||
complete: function() {
|
|
||||||
App.Documentable.unlockUploads();
|
App.Documentable.unlockUploads();
|
||||||
$(data.wrapper).remove();
|
$(this).closest(".direct-upload").remove();
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
initializeRemoveCachedDocumentLink: function(input, data) {
|
|
||||||
var remove_document_link, wrapper;
|
|
||||||
wrapper = $(input).closest(".direct-upload");
|
|
||||||
remove_document_link = $(wrapper).find("a.remove-cached-attachment");
|
|
||||||
$(remove_document_link).on("click", function(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
App.Documentable.doDeleteCachedAttachmentRequest(this.href, data);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
removeDocument: function(id) {
|
removeDocument: function(id) {
|
||||||
|
|||||||
@@ -14,11 +14,11 @@
|
|||||||
input = $(nested_image).find(".js-image-attachment");
|
input = $(nested_image).find(".js-image-attachment");
|
||||||
App.Imageable.initializeDirectUploadInput(input);
|
App.Imageable.initializeDirectUploadInput(input);
|
||||||
});
|
});
|
||||||
|
App.Imageable.initializeRemoveCachedImageLinks();
|
||||||
},
|
},
|
||||||
initializeDirectUploadInput: function(input) {
|
initializeDirectUploadInput: function(input) {
|
||||||
var inputData;
|
var inputData;
|
||||||
inputData = this.buildData([], input);
|
inputData = this.buildData([], input);
|
||||||
this.initializeRemoveCachedImageLink(input, inputData);
|
|
||||||
$(input).fileupload({
|
$(input).fileupload({
|
||||||
paramName: "attachment",
|
paramName: "attachment",
|
||||||
formData: null,
|
formData: null,
|
||||||
@@ -56,11 +56,6 @@
|
|||||||
App.Imageable.setPreview(data);
|
App.Imageable.setPreview(data);
|
||||||
destroyAttachmentLink = $(data.result.destroy_link);
|
destroyAttachmentLink = $(data.result.destroy_link);
|
||||||
$(data.destroyAttachmentLinkContainer).html(destroyAttachmentLink);
|
$(data.destroyAttachmentLinkContainer).html(destroyAttachmentLink);
|
||||||
$(destroyAttachmentLink).on("click", function(event) {
|
|
||||||
event.preventDefault();
|
|
||||||
event.stopPropagation();
|
|
||||||
App.Imageable.doDeleteCachedAttachmentRequest(this.href, data);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
progress: function(e, data) {
|
progress: function(e, data) {
|
||||||
var progress;
|
var progress;
|
||||||
@@ -122,28 +117,10 @@
|
|||||||
data.preview = $(data.wrapper).find(".image-preview");
|
data.preview = $(data.wrapper).find(".image-preview");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
doDeleteCachedAttachmentRequest: function(url, data) {
|
initializeRemoveCachedImageLinks: function() {
|
||||||
$.ajax({
|
$("#nested-image").on("ajax:complete", "a.remove-cached-attachment", function() {
|
||||||
type: "POST",
|
|
||||||
url: url,
|
|
||||||
dataType: "json",
|
|
||||||
data: {
|
|
||||||
"_method": "delete"
|
|
||||||
},
|
|
||||||
complete: function() {
|
|
||||||
$("#new_image_link").removeClass("hide");
|
$("#new_image_link").removeClass("hide");
|
||||||
$(data.wrapper).remove();
|
$(this).closest(".direct-upload").remove();
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
initializeRemoveCachedImageLink: function(input, data) {
|
|
||||||
var remove_image_link, wrapper;
|
|
||||||
wrapper = $(input).closest(".direct-upload");
|
|
||||||
remove_image_link = $(wrapper).find("a.remove-cached-attachment");
|
|
||||||
$(remove_image_link).on("click", function(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
App.Imageable.doDeleteCachedAttachmentRequest(this.href, data);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
removeImage: function(id) {
|
removeImage: function(id) {
|
||||||
|
|||||||
Reference in New Issue
Block a user