From d45d036ecbdafa5655528ebe80acefa1aae7eb14 Mon Sep 17 00:00:00 2001 From: Mikkel Hoegh Date: Sun, 26 Jan 2014 18:52:16 +0100 Subject: [PATCH] Refactor click handlers on upload button. closes #2028 - Instead of binding and unbinding the click event, bind the data to the outer scope so we can use a standard click handler. - Use removeProp instead of setting the property to false when enabling. - Use the `disabled` as value when disabling. --- core/client/views/debug.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/core/client/views/debug.js b/core/client/views/debug.js index 1b14616bc7..81fc29eba1 100644 --- a/core/client/views/debug.js +++ b/core/client/views/debug.js @@ -5,12 +5,17 @@ Ghost.Views.Debug = Ghost.View.extend({ events: { "click .settings-menu a": "handleMenuClick", + "click #startupload": "handleUploadClick", "click .js-delete": "handleDeleteClick" }, initialize: function () { + var view = this; + + this.uploadButton = this.$el.find('#startupload'); + // Disable import button and initizalize BlueImp file upload - $('#startupload').prop('disabled', true); + this.uploadButton.prop('disabled', 'disabled'); $('#importfile').fileupload({ url: Ghost.paths.apiRoot + '/db/', limitMultiFileUploads: 1, @@ -21,16 +26,12 @@ dataType: 'json', add: function (e, data) { /*jslint unparam:true*/ - // unregister click event to preveng duplicate binding - $('#startupload').off("click"); - data.context = $('#startupload').prop('disabled', false) - .click(function () { - $('#startupload').prop('disabled', true); - data.context = $('#startupload').text('Importing'); - data.submit(); - // unregister click event to allow different subsequent uploads - $('#startupload').off('click'); - }); + + // Bind the upload data to the view, so it is + // available to the click handler, and enable the + // upload button. + view.fileUploadData = data; + data.context = view.uploadButton.removeProp('disabled'); }, done: function (e, data) { /*jslint unparam:true*/ @@ -77,6 +78,18 @@ return false; }, + handleUploadClick: function (ev) { + ev.preventDefault(); + + if (!this.uploadButton.prop('disabled')) { + this.fileUploadData.context = this.uploadButton.text('Importing'); + this.fileUploadData.submit(); + } + + // Prevent double post by disabling the button. + this.uploadButton.prop('disabled', 'disabled'); + }, + handleDeleteClick: function (ev) { ev.preventDefault(); this.addSubview(new Ghost.Views.Modal({ @@ -141,4 +154,4 @@ })); } }); -}()); \ No newline at end of file +}());