From f07df02087ac3d7b7d37f86a5134c590a32ca3ee Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 14 Oct 2009 21:43:50 -0400 Subject: [PATCH] Making JsHelper::set() work well with setting the variables to an object property. --- cake/libs/view/helpers/js.php | 3 ++- cake/tests/cases/libs/view/helpers/js.test.php | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cake/libs/view/helpers/js.php b/cake/libs/view/helpers/js.php index a91a47b08..14740df48 100644 --- a/cake/libs/view/helpers/js.php +++ b/cake/libs/view/helpers/js.php @@ -237,7 +237,8 @@ class JsHelper extends AppHelper { **/ function _createVars() { if (!empty($this->__jsVars)) { - $this->buffer('var ' . $this->setVariable . ' = ' . $this->object($this->__jsVars) . ';'); + $setVar = (strpos($this->setVariable, '.')) ? $this->setVariable : 'var ' . $this->setVariable; + $this->buffer($setVar . ' = ' . $this->object($this->__jsVars) . ';'); } } diff --git a/cake/tests/cases/libs/view/helpers/js.test.php b/cake/tests/cases/libs/view/helpers/js.test.php index f7b2bbe4d..a73afeab0 100644 --- a/cake/tests/cases/libs/view/helpers/js.test.php +++ b/cake/tests/cases/libs/view/helpers/js.test.php @@ -442,13 +442,20 @@ CODE; $result = $this->Js->getBuffer(); $expected = 'var app = {"loggedIn":true,"height":"tall","color":"purple"};'; $this->assertEqual($result[0], $expected); - + $this->Js->set('loggedIn', true); $this->Js->set(array('height' => 'tall', 'color' => 'purple')); $this->Js->setVariable = 'WICKED'; - $result = $this->Js->getBuffer(false); + $result = $this->Js->getBuffer(); $expected = 'var WICKED = {"loggedIn":true,"height":"tall","color":"purple"};'; $this->assertEqual($result[0], $expected); + + $this->Js->set('loggedIn', true); + $this->Js->set(array('height' => 'tall', 'color' => 'purple')); + $this->Js->setVariable = 'Application.variables'; + $result = $this->Js->getBuffer(); + $expected = 'Application.variables = {"loggedIn":true,"height":"tall","color":"purple"};'; + $this->assertEqual($result[0], $expected); } }