mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-03-12 20:49:50 +00:00
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:
parent
00c6f22532
commit
105b17799a
2 changed files with 42 additions and 18 deletions
|
@ -473,7 +473,9 @@ class AjaxHelper extends AppHelper {
|
|||
*/
|
||||
function div($id, $options = array()) {
|
||||
if (env('HTTP_X_UPDATE') != null) {
|
||||
$this->Javascript->enabled = false;
|
||||
$divs = explode(' ', env('HTTP_X_UPDATE'));
|
||||
|
||||
if (in_array($id, $divs)) {
|
||||
@ob_end_clean();
|
||||
ob_start();
|
||||
|
@ -839,6 +841,11 @@ class AjaxHelper extends AppHelper {
|
|||
|
||||
e($this->Javascript->codeBlock($out, false));
|
||||
}
|
||||
$scripts = $this->Javascript->getCache();
|
||||
|
||||
if (!empty($scripts)) {
|
||||
e($this->Javascript->codeBlock($scripts, false));
|
||||
}
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,6 +43,7 @@ class JavascriptHelper extends AppHelper {
|
|||
var $_cacheToFile = false;
|
||||
var $_cacheAll = false;
|
||||
var $_rules = array();
|
||||
var $enabled = true;
|
||||
var $safe = false;
|
||||
/**
|
||||
* html tags used by this helper.
|
||||
|
@ -244,6 +245,34 @@ class JavascriptHelper extends AppHelper {
|
|||
$this->_cacheToFile = $file;
|
||||
$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
|
||||
*
|
||||
|
@ -255,25 +284,10 @@ class JavascriptHelper extends AppHelper {
|
|||
$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}";
|
||||
}
|
||||
$this->_cacheEvents = true;
|
||||
}
|
||||
|
||||
if ($this->_cacheEvents) {
|
||||
$this->_cacheEvents = false;
|
||||
$events = $this->_cachedEvents;
|
||||
$data = implode("\n", $events);
|
||||
$this->_cachedEvents = array();
|
||||
$data = $this->getCache();
|
||||
|
||||
if (!empty($rules)) {
|
||||
$data .= "\nvar Rules = {\n" . implode(",\n\n", $rules) . "\n}";
|
||||
$data .= "\nEventSelectors.start(Rules);\n";
|
||||
}
|
||||
|
||||
if (!empty($events) || !empty($rules)) {
|
||||
if (!empty($data)) {
|
||||
if ($this->_cacheToFile) {
|
||||
$filename = md5($data);
|
||||
if (!file_exists(JS . $filename . '.js')) {
|
||||
|
@ -281,7 +295,7 @@ class JavascriptHelper extends AppHelper {
|
|||
}
|
||||
$out = $this->link($filename);
|
||||
} else {
|
||||
$out = $this->codeBlock("\n" . $data . "\n", true);
|
||||
$out = $this->codeBlock("\n" . $data . "\n", false);
|
||||
}
|
||||
if ($inline) {
|
||||
return $out;
|
||||
|
@ -390,6 +404,9 @@ class JavascriptHelper extends AppHelper {
|
|||
* @return null
|
||||
*/
|
||||
function afterRender() {
|
||||
if (!$this->enabled) {
|
||||
return;
|
||||
}
|
||||
echo $this->writeEvents(true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue