From e323a1b98a2dc86bbd14728265f8ccd85e9f5ef9 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 4 Feb 2017 15:42:43 +1100 Subject: [PATCH] Added handleFlashMessage function to framework.js - Used for handling flash messages via JS API Request options are now passed to ajaxBeforeSend & ajaxPromise events Improved framework extras - Fixes data-request-flash when used within a plain form, this code would fail because $triggerEl is set to a plain form: `
` --- modules/system/assets/js/framework.extras.js | 13 ++++-------- modules/system/assets/js/framework.js | 21 +++++++++++++++----- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/modules/system/assets/js/framework.extras.js b/modules/system/assets/js/framework.extras.js index 4147b7d19..74d63c073 100644 --- a/modules/system/assets/js/framework.extras.js +++ b/modules/system/assets/js/framework.extras.js @@ -19,18 +19,13 @@ // FLASH HANDLING // ============================ - $(document).on('ajaxError', '[data-request][data-request-flash]', function(event, context, message) { - if (!event.isDefaultPrevented() && message) { + $(document).on('ajaxPromise', '[data-request][data-request-flash]', function(event, context, options) { + options.handleErrorMessage = function(message) { $.oc.flashMsg({ text: message, class: 'error' }) - event.preventDefault() } - }) - $(document).on('ajaxBeforeUpdate', '[data-request][data-request-flash]', function(event, context, data) { - if (data['X_OCTOBER_FLASH_MESSAGES']) { - $.each(data['X_OCTOBER_FLASH_MESSAGES'], function(type, message) { - $.oc.flashMsg({ text: message, class: type }) - }) + 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 7423ba605..72708f546 100644 --- a/modules/system/assets/js/framework.js +++ b/modules/system/assets/js/framework.js @@ -59,7 +59,8 @@ if (window.jQuery === undefined) $triggerEl = !!$form.length ? $form : $el, context = { handler: handler, options: options }, loading = options.loading !== undefined ? options.loading : null, - isRedirect = options.redirect !== undefined && options.redirect.length + isRedirect = options.redirect !== undefined && options.redirect.length, + useFlash = options.flash !== undefined var _event = jQuery.Event('oc.beforeRequest') $triggerEl.trigger(_event, context) @@ -85,7 +86,7 @@ if (window.jQuery === undefined) 'X-OCTOBER-REQUEST-PARTIALS': this.extractPartials(options.update) } - if (options.flash !== undefined) { + if (useFlash) { requestHeaders['X-OCTOBER-REQUEST-FLASH'] = 1 } @@ -111,6 +112,12 @@ if (window.jQuery === undefined) $triggerEl.trigger(_event, [context, data, textStatus, jqXHR]) if (_event.isDefaultPrevented()) return + if (useFlash && data['X_OCTOBER_FLASH_MESSAGES']) { + $.each(data['X_OCTOBER_FLASH_MESSAGES'], function(type, message) { + requestOptions.handleFlashMessage(message, type) + }) + } + /* * Proceed with the update process */ @@ -188,6 +195,11 @@ if (window.jQuery === undefined) if (message) alert(message) }, + /* + * Custom function, display a flash message to the user + */ + handleFlashMessage: function(message) {}, + /* * Custom function, handle any application specific response values * Using a promisary object here in case injected assets need time to load @@ -280,13 +292,12 @@ if (window.jQuery === undefined) context.error = requestOptions.error context.complete = requestOptions.complete requestOptions = $.extend(requestOptions, options) - 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) { if (!isRedirect) {