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] 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