Skip to content
Permalink
Browse files

Refactoring writeScripts to buffer() methods to clarify API.

  • Loading branch information...
markstory committed Mar 29, 2009
1 parent e2918c6 commit 8eeb00b66ece5e27165363dff649494a7d9a1105
Showing with 24 additions and 21 deletions.
  1. +16 −13 cake/libs/view/helpers/js.php
  2. +8 −8 cake/tests/cases/libs/view/helpers/js.test.php
@@ -33,11 +33,11 @@
**/
class JsHelper extends AppHelper {
/**
* Whether or not you want scripts to be cached or output.
* Whether or not you want scripts to be buffered or output.
*
* @var boolean
**/
var $cacheScripts = true;
var $bufferScripts = true;
/**
* helpers
*
@@ -49,7 +49,7 @@ class JsHelper extends AppHelper {
*
* @var array
**/
var $__cachedScripts = array();
var $__bufferedScripts = array();
/**
* Current Javascript Engine that is being used
*
@@ -103,9 +103,12 @@ function __construct($settings = array()) {
function call__($method, $params) {
if (isset($this->{$this->__engineName}) && method_exists($this->{$this->__engineName}, $method)) {
$out = $this->{$this->__engineName}->dispatchMethod($method, $params);
if ($this->cacheScripts) {
$this->writeCache($out);
return null;
if ($this->bufferScripts && is_string($out)) {
$this->buffer($out);
return $out;
}
if (is_object($out) && is_a($out, 'JsBaseEngineHelper')) {
return $this;
}
return $out;
}
@@ -130,10 +133,10 @@ function call__($method, $params) {
* @param array $options options for the code block
* @return string completed javascript tag.
**/
function writeScripts($options = array()) {
function writeBuffer($options = array()) {
$defaults = array('onDomReady' => true, 'inline' => true, 'cache' => false, 'clear' => true, 'safe' => true);
$options = array_merge($defaults, $options);
$script = implode("\n", $this->getCache($options['clear']));
$script = implode("\n", $this->getBuffer($options['clear']));
if ($options['onDomReady']) {
$script = $this->{$this->__engineName}->domReady($script);
@@ -157,19 +160,19 @@ function writeScripts($options = array()) {
*
* @return void
**/
function writeCache($script) {
$this->__cachedScripts[] = $script;
function buffer($script) {
$this->__bufferedScripts[] = $script;
}
/**
* Get all the cached scripts
*
* @param boolean $clear Whether or not to clear the script caches
* @return array Array of scripts added to the request.
**/
function getCache($clear = true) {
$scripts = $this->__cachedScripts;
function getBuffer($clear = true) {
$scripts = $this->__bufferedScripts;
if ($clear) {
$this->__cachedScripts = array();
$this->__bufferedScripts = array();
}
return $scripts;
}
@@ -140,9 +140,9 @@ function testMethodDispatching() {
**/
function testWriteScriptsNoFile() {
$this->Js->JsBaseEngine = new TestJsEngineHelper();
$this->Js->writeCache('one = 1;');
$this->Js->writeCache('two = 2;');
$result = $this->Js->writeScripts(array('onDomReady' => false, 'cache' => false));
$this->Js->buffer('one = 1;');
$this->Js->buffer('two = 2;');
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
@@ -153,11 +153,11 @@ function testWriteScriptsNoFile() {
$this->assertTags($result, $expected, true);
$this->Js->JsBaseEngine->expectAtLeastOnce('domReady');
$result = $this->Js->writeScripts(array('onDomReady' => true, 'cache' => false));
$result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false));
$view =& new JsHelperMockView();
$view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s=\s1;\ntwo\=\2;/')));
$result = $this->Js->writeScripts(array('onDomReady' => false, 'inline' => false, 'cache' => false));
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false));
}
/**
* test that writeScripts makes files, and puts the events into them.
@@ -169,9 +169,9 @@ function testWriteScriptsInFile() {
return;
}
$this->Js->JsBaseEngine = new TestJsEngineHelper();
$this->Js->writeCache('one = 1;');
$this->Js->writeCache('two = 2;');
$result = $this->Js->writeScripts(array('onDomReady' => false, 'cache' => true));
$this->Js->buffer('one = 1;');
$this->Js->buffer('two = 2;');
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
$expected = array(
'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'),
);

0 comments on commit 8eeb00b

Please sign in to comment.
You can’t perform that action at this time.