Skip to content
Permalink
Browse files

Adding JsHelper::set() + test cases.

  • Loading branch information...
markstory committed Oct 15, 2009
1 parent 1945204 commit 4be98b6ed990b443f192686ecbe7d014fbef7bf0
Showing with 69 additions and 10 deletions.
  1. +49 −10 cake/libs/view/helpers/js.php
  2. +20 −0 cake/tests/cases/libs/view/helpers/js.test.php
@@ -43,6 +43,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
*
@@ -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,13 +221,26 @@ 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
@@ -272,6 +285,32 @@ function link($title, $url = null, $options = array()) {
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
* element that is enhanced with Javascript. Options can include
@@ -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);
}
}

0 comments on commit 4be98b6

Please sign in to comment.
You can’t perform that action at this time.