Skip to content
Permalink
Browse files

Starting request() for jquery engine.

Adding _mapOptions so engines can map from abstract to concrete option
names.
Tests added.
  • Loading branch information...
markstory committed Mar 14, 2009
1 parent 12cc01e commit ae2114dcc17fb2ee3482034a4865cd8d0fa8894b
@@ -101,5 +101,16 @@ function effect($name, $options = array()) {
}
return $this->selection . $effect;
}
/**
* Create an $.ajax() call.
*
* @param mixed $url
* @param array $options
* @return string The completed ajax call.
**/
function request($url, $options = array()) {
$url = $this->url($url);
$options = $this->_mapOptions('request', $options);
}
}
?>
@@ -271,6 +271,12 @@ class JsBaseEngineHelper extends AppHelper {
* @access public
**/
var $selection;
/**
* Collection of option maps.
*
* @var array
**/
var $_optionMap = array();
/**
* Scripts that are queued for output
*
@@ -585,6 +591,27 @@ function _parseOptions($options) {
}
return join(', ', $out);
}
/**
* Maps Abstract options to engine specific option names.
* If attributes are missing from the map, they are not changed.
*
* @param string $method Name of method whose options are being worked with.
* @param array $options Array of options to map.
* @return array Array of mapped options.
* @access protected
**/
function _mapOptions($method, $options) {
if (!isset($this->_optionMap[$method])) {
return $options;
}
foreach ($this->_optionMap[$method] as $abstract => $concrete) {
if (isset($options[$abstract])) {
$options[$concrete] = $options[$abstract];
unset($options[$abstract]);
}
}
return $options;
}
}
@@ -126,5 +126,25 @@ function testEffect() {
$expected = "\$('#foo').toggle();";
$this->assertEqual($result, $expected);
}
/**
* Test Request Generation
*
* @return void
**/
function testRequest() {
$result = $this->Jquery->request(array('controller' => 'posts', 'action' => 'view', 1));
$expected = '$.ajax({url:"/posts/view/1"});';
$this->assertEqual($result, $expected);
$result = $this->Jquery->request('/people/edit/1', array(
'method' => 'post',
'complete' => 'doSuccess',
'error' => 'handleError',
'type' => 'json',
'data' => array('name' => 'jim', 'height' => '185cm')
));
$expected = '$.ajax({url:"/people/edit/1", method:"post", success:doSuccess, error:handleError, dataType:"json", data:"name=jim&height=185cm"});';
$this->assertEqual($result, $expected);
}
}
?>
@@ -30,6 +30,24 @@
Mock::generate('JsBaseEngineHelper', 'TestJsEngineHelper', array('methodOne'));
Mock::generate('View', 'JsHelperMockView');
class OptionEngineHelper extends JsBaseEngineHelper {
var $_optionMap = array(
'request' => array(
'complete' => 'success',
'request' => 'beforeSend',
'type' => 'dataType'
)
);
/**
* test method for testing option mapping
*
* @return array
**/
function testMap($options = array()) {
return $this->_mapOptions('request', $options);
}
}
/**
* JsHelper TestCase.
*
@@ -284,7 +302,22 @@ function testObject() {
$result = $this->JsEngine->object($data);
$this->assertEqual($result, $expected);
}
}
/**
* test Mapping of options.
*
* @return void
**/
function testOptionMapping() {
$JsEngine = new OptionEngineHelper();
$result = $JsEngine->testMap();
$this->assertEqual($result, array());
$result = $JsEngine->testMap(array('foo' => 'bar', 'baz' => 'sho'));
$this->assertEqual($result, array('foo' => 'bar', 'baz' => 'sho'));
$result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
}
}

0 comments on commit ae2114d

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