Dedicated ajaxSetup event for modifying option
Allow overriding confirm message with handleConfirmMessage function
This commit is contained in:
parent
7bcc31eef8
commit
88ed5c2c64
|
|
@ -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 })
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue