Fixing conflict between JS script caching and dynamic updating of 'magic' Ajax div's

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4551 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-02-21 22:13:16 +00:00
parent 00c6f22532
commit 105b17799a
2 changed files with 42 additions and 18 deletions
cake/libs/view/helpers

View file

@ -473,7 +473,9 @@ class AjaxHelper extends AppHelper {
*/ */
function div($id, $options = array()) { function div($id, $options = array()) {
if (env('HTTP_X_UPDATE') != null) { if (env('HTTP_X_UPDATE') != null) {
$this->Javascript->enabled = false;
$divs = explode(' ', env('HTTP_X_UPDATE')); $divs = explode(' ', env('HTTP_X_UPDATE'));
if (in_array($id, $divs)) { if (in_array($id, $divs)) {
@ob_end_clean(); @ob_end_clean();
ob_start(); ob_start();
@ -839,6 +841,11 @@ class AjaxHelper extends AppHelper {
e($this->Javascript->codeBlock($out, false)); e($this->Javascript->codeBlock($out, false));
} }
$scripts = $this->Javascript->getCache();
if (!empty($scripts)) {
e($this->Javascript->codeBlock($scripts, false));
}
exit(); exit();
} }
} }

View file

@ -43,6 +43,7 @@ class JavascriptHelper extends AppHelper {
var $_cacheToFile = false; var $_cacheToFile = false;
var $_cacheAll = false; var $_cacheAll = false;
var $_rules = array(); var $_rules = array();
var $enabled = true;
var $safe = false; var $safe = false;
/** /**
* html tags used by this helper. * html tags used by this helper.
@ -244,6 +245,34 @@ class JavascriptHelper extends AppHelper {
$this->_cacheToFile = $file; $this->_cacheToFile = $file;
$this->_cacheAll = $all; $this->_cacheAll = $all;
} }
/**
* Gets (and clears) the current JavaScript event cache
*
* @param boolean $clear
* @return string
*/
function getCache($clear = true) {
$out = '';
$rules = array();
if (!empty($this->_rules)) {
foreach ($this->_rules as $sel => $event) {
$rules[] = "\t'{$sel}': function(element, event) {\n\t\t{$event}\n\t}";
}
}
$data = implode("\n", $this->_cachedEvents);
if (!empty($rules)) {
$data .= "\nvar Rules = {\n" . implode(",\n\n", $rules) . "\n}";
$data .= "\nEventSelectors.start(Rules);\n";
}
if ($clear) {
$this->_rules = array();
$this->_cacheEvents = false;
$this->_cachedEvents = array();
}
return $data;
}
/** /**
* Write cached JavaScript events * Write cached JavaScript events
* *
@ -255,25 +284,10 @@ class JavascriptHelper extends AppHelper {
$out = ''; $out = '';
$rules = array(); $rules = array();
if (!empty($this->_rules)) {
foreach ($this->_rules as $sel => $event) {
$rules[] = "\t'{$sel}': function(element, event) {\n\t\t{$event}\n\t}";
}
$this->_cacheEvents = true;
}
if ($this->_cacheEvents) { if ($this->_cacheEvents) {
$this->_cacheEvents = false; $data = $this->getCache();
$events = $this->_cachedEvents;
$data = implode("\n", $events);
$this->_cachedEvents = array();
if (!empty($rules)) { if (!empty($data)) {
$data .= "\nvar Rules = {\n" . implode(",\n\n", $rules) . "\n}";
$data .= "\nEventSelectors.start(Rules);\n";
}
if (!empty($events) || !empty($rules)) {
if ($this->_cacheToFile) { if ($this->_cacheToFile) {
$filename = md5($data); $filename = md5($data);
if (!file_exists(JS . $filename . '.js')) { if (!file_exists(JS . $filename . '.js')) {
@ -281,7 +295,7 @@ class JavascriptHelper extends AppHelper {
} }
$out = $this->link($filename); $out = $this->link($filename);
} else { } else {
$out = $this->codeBlock("\n" . $data . "\n", true); $out = $this->codeBlock("\n" . $data . "\n", false);
} }
if ($inline) { if ($inline) {
return $out; return $out;
@ -390,6 +404,9 @@ class JavascriptHelper extends AppHelper {
* @return null * @return null
*/ */
function afterRender() { function afterRender() {
if (!$this->enabled) {
return;
}
echo $this->writeEvents(true); echo $this->writeEvents(true);
} }
} }