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:
`<form><button data-request="..." data-request-flash>...</button></form>`
This commit is contained in:
Samuel Georges 2017-02-04 15:42:43 +11:00
parent 08c5a27e50
commit e323a1b98a
2 changed files with 20 additions and 14 deletions

View File

@ -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 })
}
})

View File

@ -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) {