Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixing JsHelper::writeBuffer when inline is set to false. Scripts are…

… now enclosed in a script tag. Fixes #130
  • Loading branch information...
commit 42fa6ff04a085714c77e77c18ce82c77058ca6d9 1 parent 3eaff3e
@markstory markstory authored
View
9 cake/libs/view/helpers/js.php
@@ -199,11 +199,13 @@ function writeBuffer($options = array()) {
if ($options['onDomReady']) {
$script = $this->{$this->__engineName}->domReady($script);
}
+ $opts = $options;
+ unset($opts['onDomReady'], $opts['cache'], $opts['clear']);
+
if (!$options['cache'] && $options['inline']) {
- $opts = $options;
- unset($opts['onDomReady'], $opts['cache'], $opts['clear']);
return $this->Html->scriptBlock($script, $opts);
}
+
if ($options['cache'] && $options['inline']) {
$filename = md5($script);
if (!file_exists(JS . $filename . '.js')) {
@@ -211,8 +213,7 @@ function writeBuffer($options = array()) {
}
return $this->Html->script($filename);
}
- $view =& ClassRegistry::getObject('view');
- $view->addScript($script);
+ $this->Html->scriptBlock($script, $opts);
return null;
}
View
22 cake/tests/cases/libs/view/helpers/js.test.php
@@ -231,6 +231,28 @@ function testWriteScriptsNoFile() {
$view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s=\s1;\ntwo\=\2;/')));
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false));
}
+ function getTests() {
+ return array('start', 'startCase', 'testWriteBufferNotInline', 'endCase', 'end');
+ }
+
+/**
+ * test that writing the buffer with inline = false includes a script tag.
+ *
+ * @return void
+ */
+ function testWriteBufferNotInline() {
+ $this->Js->set('foo', 1);
+
+ $view =& new JsHelperMockView();
+ ClassRegistry::removeObject('view');
+ ClassRegistry::addObject('view', $view);
+ $view->expectCallCount('addScript', 1);
+
+ $pattern = new PatternExpectation('#<script type="text\/javascript">window.app \= \{"foo"\:1\}\;<\/script>#');
+ $view->expectAt(0, 'addScript', array($pattern));
+
+ $result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'safe' => false));
+ }
/**
* test that writeScripts makes files, and puts the events into them.
Please sign in to comment.
Something went wrong with that request. Please try again.