From bed4027d37b717dcc9d4dad0c5c11b18d9b26358 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 8 Mar 2010 22:51:46 -0500 Subject: [PATCH] Making JsHelper::writeBuffer() use the isAjax param to hint the domReady() event. Disabling domready events when the request isAjax fixes issues in prototype and makes output code simpler. Fixes #429 --- cake/libs/view/helpers/js.php | 9 +++++++-- cake/tests/cases/libs/view/helpers/js.test.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cake/libs/view/helpers/js.php b/cake/libs/view/helpers/js.php index 6f47374f9..5ad10191d 100644 --- a/cake/libs/view/helpers/js.php +++ b/cake/libs/view/helpers/js.php @@ -185,7 +185,8 @@ class JsHelper extends AppHelper { /** * Writes all Javascript generated so far to a code block or * caches them to a file and returns a linked script. If no scripts have been - * buffered this method will return null + * buffered this method will return null. If the request is an XHR(ajax) request + * onDomReady will be set to false. As the dom is already 'ready'. * * ### Options * @@ -202,7 +203,11 @@ class JsHelper extends AppHelper { * @access public */ function writeBuffer($options = array()) { - $defaults = array('onDomReady' => true, 'inline' => true, 'cache' => false, 'clear' => true, 'safe' => true); + $domReady = isset($this->params['isAjax']) ? !$this->params['isAjax'] : true; + $defaults = array( + 'onDomReady' => $domReady, 'inline' => true, + 'cache' => false, 'clear' => true, 'safe' => true + ); $options = array_merge($defaults, $options); $script = implode("\n", $this->getBuffer($options['clear'])); diff --git a/cake/tests/cases/libs/view/helpers/js.test.php b/cake/tests/cases/libs/view/helpers/js.test.php index 3138e73d3..292bbb19e 100644 --- a/cake/tests/cases/libs/view/helpers/js.test.php +++ b/cake/tests/cases/libs/view/helpers/js.test.php @@ -255,6 +255,20 @@ class JsHelperTestCase extends CakeTestCase { $result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'safe' => false)); } +/** + * test that writeBuffer() sets domReady = false when the request is done by XHR. + * Including a domReady() when in XHR can cause issues as events aren't triggered by some libraries + * + * @return void + */ + function testWriteBufferAndXhr() { + $this->_useMock(); + $this->Js->params['isAjax'] = true; + $this->Js->buffer('alert("test");'); + $this->Js->TestJsEngine->expectCallCount('dispatchMethod', 0); + $result = $this->Js->writeBuffer(); + } + /** * test that writeScripts makes files, and puts the events into them. *