Skip to content

Commit

Permalink
Add extra hooks for before ajax updates
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed May 15, 2014
1 parent f5e180d commit f8294b8
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions modules/system/assets/js/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,23 @@ if (window.jQuery === undefined)
'X-OCTOBER-REQUEST-PARTIALS': this.extractPartials(options.update)
},
success: function(data, textStatus, jqXHR) {
this.options.beforeUpdate.apply(this, [data, textStatus, jqXHR])

/*
* Halt here if beforeUpdate() or data-request-before-update returns false
*/
if (this.options.beforeUpdate.apply(this, [context, data, textStatus, jqXHR]) === false) return
if (options.evalBeforeUpdate && eval('(function($el, context, data, textStatus, jqXHR) {'+options.evalBeforeUpdate+'}($el, context, textStatus, jqXHR))') === false) return

/*
* Trigger 'ajaxBeforeUpdate' on the form, halt if event.preventDefault() is called
*/
var _event = jQuery.Event('ajaxBeforeUpdate')
form.trigger(_event, [context, data, textStatus, jqXHR])
if (_event.isDefaultPrevented()) return

/*
* Proceed with the update process
*/
var updatePromise = requestOptions.handleUpdateResponse(data, textStatus, jqXHR)

updatePromise.done(function(){
Expand Down Expand Up @@ -99,10 +114,13 @@ if (window.jQuery === undefined)

updatePromise.done(function(){
$el.data('error-message', errorMsg)
var e = jQuery.Event( "ajaxError" )
form.trigger(e, [context, textStatus, jqXHR])
if (e.isDefaultPrevented())
return

/*
* Trigger 'ajaxError' on the form, halt if event.preventDefault() is called
*/
var _event = jQuery.Event('ajaxError')
form.trigger(_event, [context, textStatus, jqXHR])
if (_event.isDefaultPrevented()) return

/*
* Halt here if the data-request-error attribute returns false
Expand Down Expand Up @@ -224,6 +242,7 @@ if (window.jQuery === undefined)
update: {},
type : 'POST',
beforeUpdate: function(data, textStatus, jqXHR) {},
evalBeforeUpdate: null,
evalSuccess: null,
evalError: null
}
Expand All @@ -250,6 +269,7 @@ if (window.jQuery === undefined)

var $this = $(this).first()
var data = {
evalBeforeUpdate: $this.data('request-before-update'),
evalSuccess: $this.data('request-success'),
evalError: $this.data('request-error'),
confirm: $this.data('request-confirm'),
Expand Down

0 comments on commit f8294b8

Please sign in to comment.