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

This commit is contained in:
Mark Story 2010-03-08 22:51:46 -05:00
parent 41bdc48fd3
commit bed4027d37
2 changed files with 21 additions and 2 deletions

View file

@ -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']));

View file

@ -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.
*