From e0b0115f99d8b93adfb9a9494aa1273f5e2f2200 Mon Sep 17 00:00:00 2001 From: Daniel Calviño Sánchez Date: Mon, 24 Apr 2017 22:07:08 +0200 Subject: 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 | 53 +++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 36 deletions(-) diff --git a/core/js/shareitemmodel.js b/core/js/shareitemmodel.js index 15af6df2c32..8144d8faa21 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,61 +179,43 @@ 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() { - if (_.isFunction(options.complete)) { - options.complete(self); - } - }).done(function() { - self.fetch().done(function() { - if (_.isFunction(options.success)) { - options.success(self); - } - }); - }).fail(function(xhr) { - var msg = t('core', 'Error'); - var result = xhr.responseJSON; - if (result && 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')); - } - }); + }, options); }, updateShare: function(shareId, attrs, options) { - var self = this; - options = options || {}; - return $.ajax({ + return this._addOrUpdateShare({ type: 'PUT', url: this._getUrl('shares/' + encodeURIComponent(shareId)), data: attrs, dataType: 'json' - }).always(function() { + }, options); + }, + + _addOrUpdateShare: function(ajaxSettings, options) { + var self = this; + options = options || {}; + + return $.ajax( + ajaxSettings + ).always(function() { if (_.isFunction(options.complete)) { options.complete(self); } }).done(function() { - self.fetch({ - success: function() { - if (_.isFunction(options.success)) { - options.success(self); - } + self.fetch().done(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) { + if (result && result.ocs && result.ocs.meta) { msg = result.ocs.meta.message; } -- cgit v1.2.3