From 7da8a388e41789ea1b131b81f4f6731702aaa8e8 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 26 Aug 2020 09:23:41 -0400 Subject: [PATCH] Take the guesswork out of rendering clientside CRM variables The window.CRM object needs to be either created or updated depending on whether it already exists. Before: Guess whether it exists based on the region and mode. After: Actually check if it exists. --- CRM/Core/Resources.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 0fdc57cf21..265112d8f9 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -354,14 +354,10 @@ class CRM_Core_Resources { * @return string */ public function renderSetting($region = NULL) { - // On a standard page request we construct the CRM object from scratch - if (($region === 'html-header') || !self::isAjaxMode()) { - $js = 'var CRM = ' . json_encode($this->getSettings()) . ';'; - } - // For an ajax request we append to it - else { - $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');'; - } + $vars = json_encode($this->getSettings(), JSON_UNESCAPED_SLASHES); + $js = "(function(vars) { + if (window.CRM) CRM.$.extend(true, CRM, vars); else window.CRM = vars; +})($vars)"; return sprintf("\n", $js); } -- 2.25.1