Refactoring writeScripts to buffer() methods to clarify API.

This commit is contained in:
mark_story 2009-03-29 14:23:42 -04:00
parent e2918c6f5e
commit 8eeb00b66e
2 changed files with 24 additions and 21 deletions

View file

@ -33,11 +33,11 @@
**/ **/
class JsHelper extends AppHelper { 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 boolean
**/ **/
var $cacheScripts = true; var $bufferScripts = true;
/** /**
* helpers * helpers
* *
@ -49,7 +49,7 @@ class JsHelper extends AppHelper {
* *
* @var array * @var array
**/ **/
var $__cachedScripts = array(); var $__bufferedScripts = array();
/** /**
* Current Javascript Engine that is being used * Current Javascript Engine that is being used
* *
@ -103,9 +103,12 @@ class JsHelper extends AppHelper {
function call__($method, $params) { function call__($method, $params) {
if (isset($this->{$this->__engineName}) && method_exists($this->{$this->__engineName}, $method)) { if (isset($this->{$this->__engineName}) && method_exists($this->{$this->__engineName}, $method)) {
$out = $this->{$this->__engineName}->dispatchMethod($method, $params); $out = $this->{$this->__engineName}->dispatchMethod($method, $params);
if ($this->cacheScripts) { if ($this->bufferScripts && is_string($out)) {
$this->writeCache($out); $this->buffer($out);
return null; return $out;
}
if (is_object($out) && is_a($out, 'JsBaseEngineHelper')) {
return $this;
} }
return $out; return $out;
} }
@ -130,10 +133,10 @@ class JsHelper extends AppHelper {
* @param array $options options for the code block * @param array $options options for the code block
* @return string completed javascript tag. * @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); $defaults = array('onDomReady' => true, 'inline' => true, 'cache' => false, 'clear' => true, 'safe' => true);
$options = array_merge($defaults, $options); $options = array_merge($defaults, $options);
$script = implode("\n", $this->getCache($options['clear'])); $script = implode("\n", $this->getBuffer($options['clear']));
if ($options['onDomReady']) { if ($options['onDomReady']) {
$script = $this->{$this->__engineName}->domReady($script); $script = $this->{$this->__engineName}->domReady($script);
@ -157,8 +160,8 @@ class JsHelper extends AppHelper {
* *
* @return void * @return void
**/ **/
function writeCache($script) { function buffer($script) {
$this->__cachedScripts[] = $script; $this->__bufferedScripts[] = $script;
} }
/** /**
* Get all the cached scripts * Get all the cached scripts
@ -166,10 +169,10 @@ class JsHelper extends AppHelper {
* @param boolean $clear Whether or not to clear the script caches * @param boolean $clear Whether or not to clear the script caches
* @return array Array of scripts added to the request. * @return array Array of scripts added to the request.
**/ **/
function getCache($clear = true) { function getBuffer($clear = true) {
$scripts = $this->__cachedScripts; $scripts = $this->__bufferedScripts;
if ($clear) { if ($clear) {
$this->__cachedScripts = array(); $this->__bufferedScripts = array();
} }
return $scripts; return $scripts;
} }

View file

@ -140,9 +140,9 @@ class JsHelperTestCase extends CakeTestCase {
**/ **/
function testWriteScriptsNoFile() { function testWriteScriptsNoFile() {
$this->Js->JsBaseEngine = new TestJsEngineHelper(); $this->Js->JsBaseEngine = new TestJsEngineHelper();
$this->Js->writeCache('one = 1;'); $this->Js->buffer('one = 1;');
$this->Js->writeCache('two = 2;'); $this->Js->buffer('two = 2;');
$result = $this->Js->writeScripts(array('onDomReady' => false, 'cache' => false)); $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false));
$expected = array( $expected = array(
'script' => array('type' => 'text/javascript'), 'script' => array('type' => 'text/javascript'),
$this->cDataStart, $this->cDataStart,
@ -153,11 +153,11 @@ class JsHelperTestCase extends CakeTestCase {
$this->assertTags($result, $expected, true); $this->assertTags($result, $expected, true);
$this->Js->JsBaseEngine->expectAtLeastOnce('domReady'); $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 =& new JsHelperMockView();
$view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s=\s1;\ntwo\=\2;/'))); $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. * test that writeScripts makes files, and puts the events into them.
@ -169,9 +169,9 @@ class JsHelperTestCase extends CakeTestCase {
return; return;
} }
$this->Js->JsBaseEngine = new TestJsEngineHelper(); $this->Js->JsBaseEngine = new TestJsEngineHelper();
$this->Js->writeCache('one = 1;'); $this->Js->buffer('one = 1;');
$this->Js->writeCache('two = 2;'); $this->Js->buffer('two = 2;');
$result = $this->Js->writeScripts(array('onDomReady' => false, 'cache' => true)); $result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => true));
$expected = array( $expected = array(
'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'), 'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'),
); );