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:
parent
08c5a27e50
commit
e323a1b98a
|
|
@ -19,18 +19,13 @@
|
||||||
// FLASH HANDLING
|
// FLASH HANDLING
|
||||||
// ============================
|
// ============================
|
||||||
|
|
||||||
$(document).on('ajaxError', '[data-request][data-request-flash]', function(event, context, message) {
|
$(document).on('ajaxPromise', '[data-request][data-request-flash]', function(event, context, options) {
|
||||||
if (!event.isDefaultPrevented() && message) {
|
options.handleErrorMessage = function(message) {
|
||||||
$.oc.flashMsg({ text: message, class: 'error' })
|
$.oc.flashMsg({ text: message, class: 'error' })
|
||||||
event.preventDefault()
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
$(document).on('ajaxBeforeUpdate', '[data-request][data-request-flash]', function(event, context, data) {
|
options.handleFlashMessage = function(message, type) {
|
||||||
if (data['X_OCTOBER_FLASH_MESSAGES']) {
|
$.oc.flashMsg({ text: message, class: type })
|
||||||
$.each(data['X_OCTOBER_FLASH_MESSAGES'], function(type, message) {
|
|
||||||
$.oc.flashMsg({ text: message, class: type })
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,8 @@ if (window.jQuery === undefined)
|
||||||
$triggerEl = !!$form.length ? $form : $el,
|
$triggerEl = !!$form.length ? $form : $el,
|
||||||
context = { handler: handler, options: options },
|
context = { handler: handler, options: options },
|
||||||
loading = options.loading !== undefined ? options.loading : null,
|
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')
|
var _event = jQuery.Event('oc.beforeRequest')
|
||||||
$triggerEl.trigger(_event, context)
|
$triggerEl.trigger(_event, context)
|
||||||
|
|
@ -85,7 +86,7 @@ if (window.jQuery === undefined)
|
||||||
'X-OCTOBER-REQUEST-PARTIALS': this.extractPartials(options.update)
|
'X-OCTOBER-REQUEST-PARTIALS': this.extractPartials(options.update)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.flash !== undefined) {
|
if (useFlash) {
|
||||||
requestHeaders['X-OCTOBER-REQUEST-FLASH'] = 1
|
requestHeaders['X-OCTOBER-REQUEST-FLASH'] = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,6 +112,12 @@ if (window.jQuery === undefined)
|
||||||
$triggerEl.trigger(_event, [context, data, textStatus, jqXHR])
|
$triggerEl.trigger(_event, [context, data, textStatus, jqXHR])
|
||||||
if (_event.isDefaultPrevented()) return
|
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
|
* Proceed with the update process
|
||||||
*/
|
*/
|
||||||
|
|
@ -188,6 +195,11 @@ if (window.jQuery === undefined)
|
||||||
if (message) alert(message)
|
if (message) alert(message)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Custom function, display a flash message to the user
|
||||||
|
*/
|
||||||
|
handleFlashMessage: function(message) {},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Custom function, handle any application specific response values
|
* Custom function, handle any application specific response values
|
||||||
* Using a promisary object here in case injected assets need time to load
|
* 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.error = requestOptions.error
|
||||||
context.complete = requestOptions.complete
|
context.complete = requestOptions.complete
|
||||||
requestOptions = $.extend(requestOptions, options)
|
requestOptions = $.extend(requestOptions, options)
|
||||||
|
|
||||||
requestOptions.data = data.join('&')
|
requestOptions.data = data.join('&')
|
||||||
|
|
||||||
if (loading) loading.show()
|
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)
|
return $.ajax(requestOptions)
|
||||||
.fail(function(jqXHR, textStatus, errorThrown) {
|
.fail(function(jqXHR, textStatus, errorThrown) {
|
||||||
if (!isRedirect) {
|
if (!isRedirect) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue