Permalink
Browse files

Making JsHelper::writeBuffer() use the isAjax param to hint the domRe…

…ady() event. Disabling domready events when the request isAjax fixes issues in prototype and makes output code simpler. Fixes #429
  • Loading branch information...
1 parent 41bdc48 commit bed4027d37b717dcc9d4dad0c5c11b18d9b26358 @markstory markstory committed Mar 9, 2010
Showing with 21 additions and 2 deletions.
  1. +7 −2 cake/libs/view/helpers/js.php
  2. +14 −0 cake/tests/cases/libs/view/helpers/js.test.php
@@ -185,7 +185,8 @@ function value($val, $quoteString = true) {
/**
* 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 @@ function value($val, $quoteString = true) {
* @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']));
@@ -256,6 +256,20 @@ function testWriteBufferNotInline() {
}
/**
+ * 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.
*
* @return void

0 comments on commit bed4027

Please sign in to comment.