Skip to content

Commit

Permalink
Adding request() to mootools.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 16, 2009
1 parent e237eec commit 91903a4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
37 changes: 23 additions & 14 deletions cake/libs/view/helpers/mootools_engine.php
Expand Up @@ -37,7 +37,6 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
**/
var $_optionMap = array(
'request' => array(
'type' => 'dataType',
'complete' => 'onComplete',
'request' => 'onRequest',
'error' => 'onFailure'
Expand Down Expand Up @@ -157,19 +156,29 @@ function effect($name, $options = array()) {
* @return string The completed ajax call.
**/
function request($url, $options = array()) {
$result = $this->Moo->request(array('controller' => 'posts', 'action' => 'view', 1));
$expected = '$.ajax({url:"/posts/view/1"});';
$this->assertEqual($result, $expected);

$result = $this->Moo->request('/people/edit/1', array(
'method' => 'post',
'complete' => 'doSuccess',
'error' => 'handleError',
'type' => 'json',
'data' => array('name' => 'jim', 'height' => '185cm')
));
$expected = '$.ajax({method:"post", error:handleError, data:"name=jim&height=185cm", dataType:"json", success:doSuccess, url:"/people/edit/1"});';
$this->assertEqual($result, $expected);
$url = $this->url($url);
$options = $this->_mapOptions('request', $options);
$type = $data = null;
if (isset($options['type']) && strtolower($options['type']) == 'json') {
$type = '.JSON';
if (!empty($options['data'])) {
$data = $this->object($options['data']);
unset($options['data']);
}
unset($options['type']);
}
if (isset($options['update'])) {
$type = '.HTML';
if (!empty($options['data'])) {
$data = $this->_toQuerystring($options['data']);
unset($options['data']);
}
unset($options['type']);
}
$options['url'] = $url;
$callbacks = array('onComplete', 'onFailure', 'onRequest');
$options = $this->_parseOptions($options, $callbacks);
return "var jsRequest = new Request$type({{$options}}).send($data);";
}
}
?>
16 changes: 16 additions & 0 deletions cake/tests/cases/libs/view/helpers/mootools_engine.test.php
Expand Up @@ -153,7 +153,23 @@ function testEffect() {
* @return void
**/
function testRequest() {
$result = $this->Moo->request(array('controller' => 'posts', 'action' => 'view', 1));
$expected = 'var jsRequest = new Request({url:"/posts/view/1"}).send();';
$this->assertEqual($result, $expected);

$result = $this->Moo->request('/posts/view/1', array('update' => 'content'));
$expected = 'var jsRequest = new Request.HTML({update:"content", url:"/posts/view/1"}).send();';
$this->assertEqual($result, $expected);

$result = $this->Moo->request('/people/edit/1', array(
'method' => 'post',
'complete' => 'doSuccess',
'error' => 'handleError',
'type' => 'json',
'data' => array('name' => 'jim', 'height' => '185cm')
));
$expected = 'var jsRequest = new Request.JSON({method:"post", onComplete:doSuccess, onFailure:handleError, url:"/people/edit/1"}).send({"name":"jim","height":"185cm"});';
$this->assertEqual($result, $expected);
}
}
?>

0 comments on commit 91903a4

Please sign in to comment.