[feature/ajax] Refactor phpbb.ajaxify event callback

PHPBB3-10270
This commit is contained in:
Igor Wiedler 2012-02-15 19:32:53 +01:00
parent 4848d6c9e8
commit de82608e6f
2 changed files with 26 additions and 32 deletions

View file

@ -249,7 +249,7 @@ phpbb.ajaxify = function(options) {
event_name = is_form ? 'submit' : 'click'; event_name = is_form ? 'submit' : 'click';
elements.bind(event_name, function() { elements.bind(event_name, function() {
var action, data, path, that = this, $this = $(this); var action, method, data, that = this, $this = $(this);
if (!$this.attr('data-ajax')) if (!$this.attr('data-ajax'))
{ {
@ -333,45 +333,39 @@ phpbb.ajaxify = function(options) {
// If the element is a form, POST must be used and some extra data must // If the element is a form, POST must be used and some extra data must
// be taken from the form. // be taken from the form.
var run_filter = (typeof options.filter === 'function'); var run_filter = (typeof options.filter === 'function');
if (is_form) if (is_form)
{ {
action = /action\[([a-z]+)\]/.exec(this.name); action = $this.attr('action').replace('&', '&');
data = decodeURI($this.closest('form').serialize()); data = $this.serializeArray();
path = $this.closest('form').attr('action').replace('&', '&'); method = $this.attr('method');
if (action)
{
action = action[1];
data += '&action=' + action;
}
else
{
data += '&' + this.name + '=' + this.value;
}
// If filter function returns true, cancel the AJAX functionality,
// and return true (meaning that the HTTP request will be sent normally).
if (run_filter && options.filter.call($this.parents('form')[0], action, data))
{
return true;
}
phpbb.loading_alert();
$.post(path, data, return_handler);
} }
else else
{ {
// If filter function returns true, cancel the AJAX functionality, action = this.href;
// and return true (meaning that the HTTP request will be sent normally). data = null;
if (run_filter && options.filter.call(this)) method = 'GET';
{
return true;
}
phpbb.loading_alert();
$.get(this.href, return_handler);
} }
// If filter function returns true, cancel the AJAX functionality,
// and return true (meaning that the HTTP request will be sent normally).
if (run_filter && options.filter.call(this, data))
{
return true;
}
phpbb.loading_alert();
$.ajax({
url: action,
type: method,
data: data,
success: return_handler
});
return false; return false;
}); });
return this; return this;
} }

View file

@ -73,7 +73,7 @@ $('[data-ajax]').each(function() {
*/ */
phpbb.ajaxify({ phpbb.ajaxify({
selector: '#quickmodform', selector: '#quickmodform',
filter: function(act, data) { filter: function(data) {
var action = $('#quick-mod-select').val(); var action = $('#quick-mod-select').val();
if (action === 'make_normal') if (action === 'make_normal')