Added on_succes and custom options for web2py ajax function

This commit is contained in:
Carlos Cesar Caballero Díaz
2017-04-10 12:23:34 -04:00
parent ad43249f61
commit 232598fd8d
3 changed files with 60 additions and 9 deletions
+20 -3
View File
@@ -38,7 +38,7 @@
if (value > 0) $('#' + id).hide().fadeIn('slow'); if (value > 0) $('#' + id).hide().fadeIn('slow');
else $('#' + id).show().fadeOut('slow'); else $('#' + id).show().fadeOut('slow');
}, },
ajax: function (u, s, t) { ajax: function (u, s, t, options) {
/*simple ajax function*/ /*simple ajax function*/
var query = ''; var query = '';
if (typeof s == 'string') { if (typeof s == 'string') {
@@ -59,7 +59,9 @@
query = pcs.join('&'); query = pcs.join('&');
} }
} }
$.ajax({
// default options for jquery ajax function
var ajax_options = {
type: 'POST', type: 'POST',
url: u, url: u,
data: query, data: query,
@@ -69,8 +71,23 @@
else if (typeof t == 'string') $('#' + t).html(msg); else if (typeof t == 'string') $('#' + t).html(msg);
else t(msg); else t(msg);
} }
// trigger on_success
if (options.on_success){
options.on_success();
}
} }
}); };
// merge default ajax options with user custom options
for (var attrname in options) {
// not merge custom on_success option
if(attrname != "on_success"){
ajax_options[attrname] = options[attrname];
}
}
// call ajax function
$.ajax(ajax_options);
}, },
ajax_fields: function (target) { ajax_fields: function (target) {
/* /*
+20 -3
View File
@@ -38,7 +38,7 @@
if (value > 0) $('#' + id).hide().fadeIn('slow'); if (value > 0) $('#' + id).hide().fadeIn('slow');
else $('#' + id).show().fadeOut('slow'); else $('#' + id).show().fadeOut('slow');
}, },
ajax: function (u, s, t) { ajax: function (u, s, t, options) {
/*simple ajax function*/ /*simple ajax function*/
var query = ''; var query = '';
if (typeof s == 'string') { if (typeof s == 'string') {
@@ -59,7 +59,9 @@
query = pcs.join('&'); query = pcs.join('&');
} }
} }
$.ajax({
// default options for jquery ajax function
var ajax_options = {
type: 'POST', type: 'POST',
url: u, url: u,
data: query, data: query,
@@ -69,8 +71,23 @@
else if (typeof t == 'string') $('#' + t).html(msg); else if (typeof t == 'string') $('#' + t).html(msg);
else t(msg); else t(msg);
} }
// trigger on_success
if (options.on_success){
options.on_success();
}
} }
}); };
// merge default ajax options with user custom options
for (var attrname in options) {
// not merge custom on_success option
if(attrname != "on_success"){
ajax_options[attrname] = options[attrname];
}
}
// call ajax function
$.ajax(ajax_options);
}, },
ajax_fields: function (target) { ajax_fields: function (target) {
/* /*
+20 -3
View File
@@ -38,7 +38,7 @@
if (value > 0) $('#' + id).hide().fadeIn('slow'); if (value > 0) $('#' + id).hide().fadeIn('slow');
else $('#' + id).show().fadeOut('slow'); else $('#' + id).show().fadeOut('slow');
}, },
ajax: function (u, s, t) { ajax: function (u, s, t, options) {
/*simple ajax function*/ /*simple ajax function*/
var query = ''; var query = '';
if (typeof s == 'string') { if (typeof s == 'string') {
@@ -59,7 +59,9 @@
query = pcs.join('&'); query = pcs.join('&');
} }
} }
$.ajax({
// default options for jquery ajax function
var ajax_options = {
type: 'POST', type: 'POST',
url: u, url: u,
data: query, data: query,
@@ -69,8 +71,23 @@
else if (typeof t == 'string') $('#' + t).html(msg); else if (typeof t == 'string') $('#' + t).html(msg);
else t(msg); else t(msg);
} }
// trigger on_success
if (options.on_success){
options.on_success();
}
} }
}); };
// merge default ajax options with user custom options
for (var attrname in options) {
// not merge custom on_success option
if(attrname != "on_success"){
ajax_options[attrname] = options[attrname];
}
}
// call ajax function
$.ajax(ajax_options);
}, },
ajax_fields: function (target) { ajax_fields: function (target) {
/* /*