mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Adding JsHelper::set() + test cases.
This commit is contained in:
parent
47558d4fe1
commit
4a330c0920
2 changed files with 69 additions and 10 deletions
|
@ -43,6 +43,14 @@ class JsHelper extends AppHelper {
|
||||||
**/
|
**/
|
||||||
var $helpers = array('Html', 'Form');
|
var $helpers = array('Html', 'Form');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variables to pass to Javascript.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @see JsHelper::set()
|
||||||
|
**/
|
||||||
|
var $__jsVars = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scripts that are queued for output
|
* Scripts that are queued for output
|
||||||
*
|
*
|
||||||
|
@ -59,19 +67,11 @@ class JsHelper extends AppHelper {
|
||||||
var $__engineName;
|
var $__engineName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* __objects
|
* The javascript variable created by set() variables.
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
* @access private
|
|
||||||
**/
|
|
||||||
var $__objects = array();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* output
|
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
**/
|
**/
|
||||||
var $output = false;
|
var $setVariable = APP_DIR;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor - determines engine helper
|
* Constructor - determines engine helper
|
||||||
|
@ -221,13 +221,26 @@ class JsHelper extends AppHelper {
|
||||||
* @return array Array of scripts added to the request.
|
* @return array Array of scripts added to the request.
|
||||||
**/
|
**/
|
||||||
function getBuffer($clear = true) {
|
function getBuffer($clear = true) {
|
||||||
|
$this->_createVars();
|
||||||
$scripts = $this->__bufferedScripts;
|
$scripts = $this->__bufferedScripts;
|
||||||
if ($clear) {
|
if ($clear) {
|
||||||
$this->__bufferedScripts = array();
|
$this->__bufferedScripts = array();
|
||||||
|
$this->__jsVars = array();
|
||||||
}
|
}
|
||||||
return $scripts;
|
return $scripts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates the object string for variables passed to javascript.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
**/
|
||||||
|
function _createVars() {
|
||||||
|
if (!empty($this->__jsVars)) {
|
||||||
|
$this->buffer('var ' . $this->setVariable . ' = ' . $this->object($this->__jsVars) . ';');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate an 'Ajax' link. Uses the selected JS engine to create a link
|
* Generate an 'Ajax' link. Uses the selected JS engine to create a link
|
||||||
* element that is enhanced with Javascript. Options can include
|
* element that is enhanced with Javascript. Options can include
|
||||||
|
@ -272,6 +285,32 @@ class JsHelper extends AppHelper {
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pass variables into Javascript. Allows you to set variables that will be
|
||||||
|
* output when the buffer is fetched with `JsHelper::getBuffer()` or `JsHelper::writeBuffer()`
|
||||||
|
* The Javascript variable used to output set variables can be controlled with `JsHelper::$setVariable`
|
||||||
|
*
|
||||||
|
* @param mixed $one
|
||||||
|
* @param mixed $two
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function set($one, $two = null) {
|
||||||
|
$data = null;
|
||||||
|
if (is_array($one)) {
|
||||||
|
if (is_array($two)) {
|
||||||
|
$data = array_combine($one, $two);
|
||||||
|
} else {
|
||||||
|
$data = $one;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$data = array($one => $two);
|
||||||
|
}
|
||||||
|
if ($data == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$this->__jsVars = array_merge($this->__jsVars, $data);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses the selected JS engine to create a submit input
|
* Uses the selected JS engine to create a submit input
|
||||||
* element that is enhanced with Javascript. Options can include
|
* element that is enhanced with Javascript. Options can include
|
||||||
|
|
|
@ -430,6 +430,26 @@ CODE;
|
||||||
$expected = '{"one":"first","two":"second"}';
|
$expected = '{"one":"first","two":"second"}';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test set()'ing variables to the Javascript buffer and controlling the output var name.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function testSet() {
|
||||||
|
$this->Js->set('loggedIn', true);
|
||||||
|
$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
|
||||||
|
$result = $this->Js->getBuffer();
|
||||||
|
$expected = 'var app = {"loggedIn":true,"height":"tall","color":"purple"};';
|
||||||
|
$this->assertEqual($result[0], $expected);
|
||||||
|
|
||||||
|
$this->Js->set('loggedIn', true);
|
||||||
|
$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
|
||||||
|
$this->Js->setVariable = 'WICKED';
|
||||||
|
$result = $this->Js->getBuffer(false);
|
||||||
|
$expected = 'var WICKED = {"loggedIn":true,"height":"tall","color":"purple"};';
|
||||||
|
$this->assertEqual($result[0], $expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue