From 232598fd8deeb417716c8dab366f66ee4c368b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Cesar=20Caballero=20D=C3=ADaz?= Date: Mon, 10 Apr 2017 12:23:34 -0400 Subject: [PATCH 1/2] Added on_succes and custom options for web2py ajax function --- applications/admin/static/js/web2py.js | 23 ++++++++++++++++++++--- applications/examples/static/js/web2py.js | 23 ++++++++++++++++++++--- applications/welcome/static/js/web2py.js | 23 ++++++++++++++++++++--- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/applications/admin/static/js/web2py.js b/applications/admin/static/js/web2py.js index 1228961c..e98359fe 100644 --- a/applications/admin/static/js/web2py.js +++ b/applications/admin/static/js/web2py.js @@ -38,7 +38,7 @@ if (value > 0) $('#' + id).hide().fadeIn('slow'); else $('#' + id).show().fadeOut('slow'); }, - ajax: function (u, s, t) { + ajax: function (u, s, t, options) { /*simple ajax function*/ var query = ''; if (typeof s == 'string') { @@ -59,7 +59,9 @@ query = pcs.join('&'); } } - $.ajax({ + + // default options for jquery ajax function + var ajax_options = { type: 'POST', url: u, data: query, @@ -69,8 +71,23 @@ else if (typeof t == 'string') $('#' + t).html(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) { /* diff --git a/applications/examples/static/js/web2py.js b/applications/examples/static/js/web2py.js index 1228961c..e98359fe 100644 --- a/applications/examples/static/js/web2py.js +++ b/applications/examples/static/js/web2py.js @@ -38,7 +38,7 @@ if (value > 0) $('#' + id).hide().fadeIn('slow'); else $('#' + id).show().fadeOut('slow'); }, - ajax: function (u, s, t) { + ajax: function (u, s, t, options) { /*simple ajax function*/ var query = ''; if (typeof s == 'string') { @@ -59,7 +59,9 @@ query = pcs.join('&'); } } - $.ajax({ + + // default options for jquery ajax function + var ajax_options = { type: 'POST', url: u, data: query, @@ -69,8 +71,23 @@ else if (typeof t == 'string') $('#' + t).html(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) { /* diff --git a/applications/welcome/static/js/web2py.js b/applications/welcome/static/js/web2py.js index 1228961c..e98359fe 100644 --- a/applications/welcome/static/js/web2py.js +++ b/applications/welcome/static/js/web2py.js @@ -38,7 +38,7 @@ if (value > 0) $('#' + id).hide().fadeIn('slow'); else $('#' + id).show().fadeOut('slow'); }, - ajax: function (u, s, t) { + ajax: function (u, s, t, options) { /*simple ajax function*/ var query = ''; if (typeof s == 'string') { @@ -59,7 +59,9 @@ query = pcs.join('&'); } } - $.ajax({ + + // default options for jquery ajax function + var ajax_options = { type: 'POST', url: u, data: query, @@ -69,8 +71,23 @@ else if (typeof t == 'string') $('#' + t).html(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) { /* From 02d2fefc214b35184fbc2c45cb20f0da2bee3b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Cesar=20Caballero=20D=C3=ADaz?= Date: Wed, 12 Apr 2017 08:48:52 -0400 Subject: [PATCH 2/2] renamed "on_succes" option to "done" allowing multiple inputs renamed "on_succes" option to "done" allowing multiple inputs,code refactoring --- applications/admin/static/js/web2py.js | 43 +++++++++++++++-------- applications/examples/static/js/web2py.js | 43 +++++++++++++++-------- applications/welcome/static/js/web2py.js | 43 +++++++++++++++-------- 3 files changed, 84 insertions(+), 45 deletions(-) diff --git a/applications/admin/static/js/web2py.js b/applications/admin/static/js/web2py.js index e98359fe..313d00bc 100644 --- a/applications/admin/static/js/web2py.js +++ b/applications/admin/static/js/web2py.js @@ -40,6 +40,10 @@ }, ajax: function (u, s, t, options) { /*simple ajax function*/ + + // set options default value + options = typeof options !== 'undefined' ? options : {}; + var query = ''; if (typeof s == 'string') { var d = $(s).serialize(); @@ -60,30 +64,39 @@ } } - // default options for jquery ajax function + // default success action + var success_function = function (msg) { + if (t) { + if (t == ':eval') eval(msg); + else if (typeof t == 'string') $('#' + t).html(msg); + else t(msg); + } + }; + + // declare success actions as array + var success = [success_function]; + + // add user success actions + if ($.isArray(options.done)){ + success = $.merge(success, options.done); + } else { + success.push(options.done); + } + + // default jquery ajax options var ajax_options = { type: 'POST', url: u, data: query, - success: function (msg) { - if (t) { - if (t == ':eval') eval(msg); - else if (typeof t == 'string') $('#' + t).html(msg); - else t(msg); - } - // trigger on_success - if (options.on_success){ - options.on_success(); - } - } + success: success }; + //remove custom "done" option if exists + delete options.done; + // 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 diff --git a/applications/examples/static/js/web2py.js b/applications/examples/static/js/web2py.js index e98359fe..313d00bc 100644 --- a/applications/examples/static/js/web2py.js +++ b/applications/examples/static/js/web2py.js @@ -40,6 +40,10 @@ }, ajax: function (u, s, t, options) { /*simple ajax function*/ + + // set options default value + options = typeof options !== 'undefined' ? options : {}; + var query = ''; if (typeof s == 'string') { var d = $(s).serialize(); @@ -60,30 +64,39 @@ } } - // default options for jquery ajax function + // default success action + var success_function = function (msg) { + if (t) { + if (t == ':eval') eval(msg); + else if (typeof t == 'string') $('#' + t).html(msg); + else t(msg); + } + }; + + // declare success actions as array + var success = [success_function]; + + // add user success actions + if ($.isArray(options.done)){ + success = $.merge(success, options.done); + } else { + success.push(options.done); + } + + // default jquery ajax options var ajax_options = { type: 'POST', url: u, data: query, - success: function (msg) { - if (t) { - if (t == ':eval') eval(msg); - else if (typeof t == 'string') $('#' + t).html(msg); - else t(msg); - } - // trigger on_success - if (options.on_success){ - options.on_success(); - } - } + success: success }; + //remove custom "done" option if exists + delete options.done; + // 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 diff --git a/applications/welcome/static/js/web2py.js b/applications/welcome/static/js/web2py.js index e98359fe..313d00bc 100644 --- a/applications/welcome/static/js/web2py.js +++ b/applications/welcome/static/js/web2py.js @@ -40,6 +40,10 @@ }, ajax: function (u, s, t, options) { /*simple ajax function*/ + + // set options default value + options = typeof options !== 'undefined' ? options : {}; + var query = ''; if (typeof s == 'string') { var d = $(s).serialize(); @@ -60,30 +64,39 @@ } } - // default options for jquery ajax function + // default success action + var success_function = function (msg) { + if (t) { + if (t == ':eval') eval(msg); + else if (typeof t == 'string') $('#' + t).html(msg); + else t(msg); + } + }; + + // declare success actions as array + var success = [success_function]; + + // add user success actions + if ($.isArray(options.done)){ + success = $.merge(success, options.done); + } else { + success.push(options.done); + } + + // default jquery ajax options var ajax_options = { type: 'POST', url: u, data: query, - success: function (msg) { - if (t) { - if (t == ':eval') eval(msg); - else if (typeof t == 'string') $('#' + t).html(msg); - else t(msg); - } - // trigger on_success - if (options.on_success){ - options.on_success(); - } - } + success: success }; + //remove custom "done" option if exists + delete options.done; + // 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