From 28662dbed3fdcf4b5d0fd87af236bd5eda5ec8fa Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 19 Jul 2018 14:12:16 +0300 Subject: [PATCH] Form serializeobject now can convert field names to camelcase --- .../jquery/jquery-extensions.js | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js index e94dadf9a5..fe39432f06 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js @@ -59,23 +59,57 @@ }); }; - $.fn.serializeFormToObject = function () { + var toCamelCase = function(str) { + var regexs = [/(^[A-Z])/, /((\.)[A-Z])/]; + + regexs.forEach( + function(regex) { + var infLoopAvoider = 0; + + while (regex.test(str)) { + str = str + .replace(regex, function ($1) { return $1.toLowerCase(); }); + + if (infLoopAvoider++ > 1000) { + break; + } + } + } + ); + + return str; + }; + + $.fn.serializeFormToObject = function (camelCase = true) { //serialize to array var data = $(this).serializeArray(); //add also disabled items $(':disabled[name]', this) .each(function (item) { + var value = ''; + if ($(this).is(":checkbox")) { - data.push({ name: this.name, value: $(this).is(':checked') }); + value = $(this).is(':checked'); } else { - data.push({ name: this.name, value: $(this).val() }); + value = $(this).val(); } + + data.push({ name: this.name, value: value }); }); //map to object var obj = {}; - data.map(function (x) { obj[x.name] = x.value; }); + + if (camelCase) { + data.forEach(function (d) { + d.name = toCamelCase(d.name); + }); + } + + data.map(function (x) { + obj[x.name] = x.value; + }); return obj; };