From e0b0115f99d8b93adfb9a9494aa1273f5e2f2200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Mon, 24 Apr 2017 22:07:08 +0200 Subject: [PATCH] Extract common ajax call for addShare and updateShare MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Calviño Sánchez --- core/js/shareitemmodel.js | 59 +++++++++++++-------------------------- 1 file changed, 20 insertions(+), 39 deletions(-) diff --git a/core/js/shareitemmodel.js b/core/js/shareitemmodel.js index 15af6df2c3..8144d8faa2 100644 --- a/core/js/shareitemmodel.js +++ b/core/js/shareitemmodel.js @@ -156,7 +156,6 @@ addShare: function(attributes, options) { var shareType = attributes.shareType; - options = options || {}; attributes = _.extend({}, attributes); // Default permissions are Edit (CRUD) and Share @@ -180,13 +179,30 @@ attributes.path = this.fileInfoModel.getFullPath(); } - var self = this; - return $.ajax({ + return this._addOrUpdateShare({ type: 'POST', url: this._getUrl('shares'), data: attributes, dataType: 'json' - }).always(function() { + }, options); + }, + + updateShare: function(shareId, attrs, options) { + return this._addOrUpdateShare({ + type: 'PUT', + url: this._getUrl('shares/' + encodeURIComponent(shareId)), + data: attrs, + dataType: 'json' + }, options); + }, + + _addOrUpdateShare: function(ajaxSettings, options) { + var self = this; + options = options || {}; + + return $.ajax( + ajaxSettings + ).always(function() { if (_.isFunction(options.complete)) { options.complete(self); } @@ -211,41 +227,6 @@ }); }, - updateShare: function(shareId, attrs, options) { - var self = this; - options = options || {}; - return $.ajax({ - type: 'PUT', - url: this._getUrl('shares/' + encodeURIComponent(shareId)), - data: attrs, - dataType: 'json' - }).always(function() { - if (_.isFunction(options.complete)) { - options.complete(self); - } - }).done(function() { - self.fetch({ - success: function() { - if (_.isFunction(options.success)) { - options.success(self); - } - } - }); - }).fail(function(xhr) { - var msg = t('core', 'Error'); - var result = xhr.responseJSON; - if (result.ocs && result.ocs.meta) { - msg = result.ocs.meta.message; - } - - if (_.isFunction(options.error)) { - options.error(self, msg); - } else { - OC.dialogs.alert(msg, t('core', 'Error while sharing')); - } - }); - }, - /** * Deletes the share with the given id *