From 88ed5c2c6455bc906cc2679cf8403b3e9e5771aa Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 4 Feb 2017 19:44:53 +1100 Subject: [PATCH] Dedicated ajaxSetup event for modifying option Allow overriding confirm message with handleConfirmMessage function --- modules/system/assets/js/framework.extras.js | 6 +- modules/system/assets/js/framework.js | 74 ++++++++++---------- 2 files changed, 39 insertions(+), 41 deletions(-) diff --git a/modules/system/assets/js/framework.extras.js b/modules/system/assets/js/framework.extras.js index 74d63c073..149a963f4 100644 --- a/modules/system/assets/js/framework.extras.js +++ b/modules/system/assets/js/framework.extras.js @@ -19,12 +19,12 @@ // FLASH HANDLING // ============================ - $(document).on('ajaxPromise', '[data-request][data-request-flash]', function(event, context, options) { - options.handleErrorMessage = function(message) { + $(document).on('ajaxSetup', '[data-request][data-request-flash]', function(event, context) { + context.options.handleErrorMessage = function(message) { $.oc.flashMsg({ text: message, class: 'error' }) } - options.handleFlashMessage = function(message, type) { + context.options.handleFlashMessage = function(message, type) { $.oc.flashMsg({ text: message, class: type }) } }) diff --git a/modules/system/assets/js/framework.js b/modules/system/assets/js/framework.js index 5b861be75..a60dabc40 100644 --- a/modules/system/assets/js/framework.js +++ b/modules/system/assets/js/framework.js @@ -13,60 +13,32 @@ if (window.jQuery === undefined) var Request = function (element, handler, options) { var $el = this.$el = $(element); this.options = options || {}; + /* * Validate handler name */ - if (handler == undefined) throw new Error('The request handler name is not specified.') if (!handler.match(/^(?:\w+\:{2})?on*/)) throw new Error('Invalid handler name. The correct handler name format is: "onEvent".') - /* - * Custom function, requests confirmation from the user - */ - - function handleConfirmMessage(message) { - var _event = jQuery.Event('ajaxConfirmMessage') - - _event.promise = $.Deferred() - if ($(window).triggerHandler(_event, [message]) !== undefined) { - _event.promise.done(function() { - options.confirm = null - new Request(element, handler, options) - }) - return false - } - - if (_event.isDefaultPrevented()) return - if (message) return confirm(message) - } - - /* - * Initiate request - */ - - if (options.confirm && !handleConfirmMessage(options.confirm)) - return - /* * Prepare the options and execute the request */ - - var - $form = $el.closest('form'), + var $form = $el.closest('form'), $triggerEl = !!$form.length ? $form : $el, - context = { handler: handler, options: options }, - loading = options.loading !== undefined ? options.loading : null, - isRedirect = options.redirect !== undefined && options.redirect.length, - useFlash = options.flash !== undefined + context = { handler: handler, options: options } + $el.trigger('ajaxSetup', [context]) var _event = jQuery.Event('oc.beforeRequest') $triggerEl.trigger(_event, context) if (_event.isDefaultPrevented()) return - var data = [$form.serialize()] + var data = [$form.serialize()], + loading = options.loading !== undefined ? options.loading : null, + isRedirect = options.redirect !== undefined && options.redirect.length, + useFlash = options.flash !== undefined $.each($el.parents('[data-request-data]').toArray().reverse(), function extendRequest() { data.push($.param(paramToObj('data-request-data', $(this).data('request-data')))) @@ -195,6 +167,25 @@ if (window.jQuery === undefined) if (message) alert(message) }, + /* + * Custom function, requests confirmation from the user + */ + handleConfirmMessage: function(message) { + var _event = jQuery.Event('ajaxConfirmMessage') + + _event.promise = $.Deferred() + if ($(window).triggerHandler(_event, [message]) !== undefined) { + _event.promise.done(function() { + options.confirm = null + new Request(element, handler, options) + }) + return false + } + + if (_event.isDefaultPrevented()) return + if (message) return confirm(message) + }, + /* * Custom function, display a flash message to the user */ @@ -293,6 +284,13 @@ if (window.jQuery === undefined) } } + /* + * Initiate request + */ + if (options.confirm && !requestOptions.handleConfirmMessage(options.confirm)) { + return + } + /* * Allow default business logic to be called from user functions */ @@ -303,8 +301,8 @@ if (window.jQuery === undefined) requestOptions.data = data.join('&') if (loading) loading.show() - $(window).trigger('ajaxBeforeSend', [context, requestOptions]) - $el.trigger('ajaxPromise', [context, requestOptions]) + $(window).trigger('ajaxBeforeSend', [context]) + $el.trigger('ajaxPromise', [context]) return $.ajax(requestOptions) .fail(function(jqXHR, textStatus, errorThrown) {