mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Refactoring writeScripts to buffer() methods to clarify API.
This commit is contained in:
parent
e2918c6f5e
commit
8eeb00b66e
2 changed files with 24 additions and 21 deletions
|
@ -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 @@ class JsHelper extends AppHelper {
|
|||
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 @@ class JsHelper extends AppHelper {
|
|||
* @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,8 +160,8 @@ class JsHelper extends AppHelper {
|
|||
*
|
||||
* @return void
|
||||
**/
|
||||
function writeCache($script) {
|
||||
$this->__cachedScripts[] = $script;
|
||||
function buffer($script) {
|
||||
$this->__bufferedScripts[] = $script;
|
||||
}
|
||||
/**
|
||||
* Get all the cached scripts
|
||||
|
@ -166,10 +169,10 @@ class JsHelper extends AppHelper {
|
|||
* @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 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
**/
|
||||
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 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
$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 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
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/'),
|
||||
);
|
||||
|
|
Loading…
Add table
Reference in a new issue