Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding JsHelper::set() + test cases.

  • Loading branch information...
commit 4a330c0920e06f4189cb31d2fbd8c662ba3ba01a 1 parent 47558d4
@markstory markstory authored
View
59 cake/libs/view/helpers/js.php
@@ -44,6 +44,14 @@ class JsHelper extends AppHelper {
var $helpers = array('Html', 'Form');
/**
+ * Variables to pass to Javascript.
+ *
+ * @var array
+ * @see JsHelper::set()
+ **/
+ var $__jsVars = array();
+
+/**
* Scripts that are queued for output
*
* @var array
@@ -59,19 +67,11 @@ class JsHelper extends AppHelper {
var $__engineName;
/**
- * __objects
- *
- * @var array
- * @access private
- **/
- var $__objects = array();
-
-/**
- * output
+ * The javascript variable created by set() variables.
*
* @var string
**/
- var $output = false;
+ var $setVariable = APP_DIR;
/**
* Constructor - determines engine helper
@@ -221,14 +221,27 @@ function buffer($script) {
* @return array Array of scripts added to the request.
**/
function getBuffer($clear = true) {
+ $this->_createVars();
$scripts = $this->__bufferedScripts;
if ($clear) {
$this->__bufferedScripts = array();
+ $this->__jsVars = array();
}
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
* element that is enhanced with Javascript. Options can include
* both those for HtmlHelper::link() and JsBaseEngine::request(), JsBaseEngine::event();
@@ -273,6 +286,32 @@ function link($title, $url = null, $options = array()) {
}
/**
+ * 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
* element that is enhanced with Javascript. Options can include
* both those for FormHelper::submit() and JsBaseEngine::request(), JsBaseEngine::event();
View
20 cake/tests/cases/libs/view/helpers/js.test.php
@@ -430,6 +430,26 @@ function testObjectPassThrough() {
$expected = '{"one":"first","two":"second"}';
$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);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.