Skip to content

Commit

Permalink
Fixing request() callbacks. Starting test for prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 6, 2009
1 parent 87c5a5d commit 5f7abae
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 11 deletions.
3 changes: 1 addition & 2 deletions cake/libs/view/helpers/jquery_engine.php
Expand Up @@ -35,8 +35,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
var $_optionMap = array(
'request' => array(
'type' => 'dataType',
'complete' => 'success',
'request' => 'beforeSend',
'before' => 'beforeSend',
),
'sortable' => array(
'complete' => 'stop',
Expand Down
3 changes: 2 additions & 1 deletion cake/libs/view/helpers/js.php
Expand Up @@ -562,7 +562,8 @@ function effect($name, $options) {
* ### Event Options
*
* - 'complete' - Callback to fire on complete.
* - 'request' - Callback to fire on request initialization.
* - 'success' - Callback to fire on success.
* - 'before' - Callback to fire on request initialization.
* - 'error' - Callback to fire on request failure.
*
* ### Options
Expand Down
5 changes: 3 additions & 2 deletions cake/libs/view/helpers/mootools_engine.php
Expand Up @@ -38,7 +38,8 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
var $_optionMap = array(
'request' => array(
'complete' => 'onComplete',
'request' => 'onRequest',
'success' => 'onSuccess',
'before' => 'onRequest',
'error' => 'onFailure'
),
'sortable' => array(
Expand Down Expand Up @@ -185,7 +186,7 @@ function request($url, $options = array()) {
unset($options['type']);
}
$options['url'] = $url;
$callbacks = array('onComplete', 'onFailure', 'onRequest');
$callbacks = array('onComplete', 'onFailure', 'onRequest', 'onSuccess', 'onCancel', 'onException');
$options = $this->_parseOptions($options, $callbacks);
return "var jsRequest = new Request$type({{$options}}).send($data);";
}
Expand Down
35 changes: 33 additions & 2 deletions cake/libs/view/helpers/prototype_engine.php
Expand Up @@ -34,7 +34,13 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
* @var array
**/
var $_optionMap = array(

'request' => array(
'async' => 'asyncrhronous',
'data' => 'parameters',
'before' => 'onCreate',
'complete' => 'onSuccess',
'error' => 'onFailure'
)
);
/**
* Create javascript selector for a CSS rule
Expand Down Expand Up @@ -153,7 +159,32 @@ function effect($name, $options = array()) {
* @return string The completed ajax call.
**/
function request($url, $options = array()) {

$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'])) {
$options['update'] = str_replace('#', '', $options['update']);
$type = '.Updater';
if (!empty($options['data'])) {
$data = $this->_toQuerystring($options['data']);
unset($options['data']);
}
unset($options['type']);
}
$callbacks = array('onComplete', 'onFailure', 'onRequest');
$options = $this->_parseOptions($options, $callbacks);
if (!empty($options)) {
$options = ', {' . $options . '}';
}
return "var jsRequest = new Ajax$type($url$options);";
}
/**
* Create a sortable element.
Expand Down
6 changes: 4 additions & 2 deletions cake/tests/cases/libs/view/helpers/jquery_engine.test.php
Expand Up @@ -146,12 +146,14 @@ function testRequest() {

$result = $this->Jquery->request('/people/edit/1', array(
'method' => 'post',
'complete' => 'doSuccess',
'before' => 'doBefore',
'complete' => 'doComplete',
'success' => 'doSuccess',
'error' => 'handleError',
'type' => 'json',
'data' => array('name' => 'jim', 'height' => '185cm')
));
$expected = '$.ajax({data:"name=jim&height=185cm", dataType:"json", error:handleError, method:"post", success:doSuccess, url:"/people/edit/1"});';
$expected = '$.ajax({beforeSend:doBefore, complete:doComplete, data:"name=jim&height=185cm", dataType:"json", error:handleError, method:"post", success:doSuccess, url:"/people/edit/1"});';
$this->assertEqual($result, $expected);

$result = $this->Jquery->request('/people/edit/1', array(
Expand Down
7 changes: 5 additions & 2 deletions cake/tests/cases/libs/view/helpers/mootools_engine.test.php
Expand Up @@ -181,10 +181,13 @@ function testRequest() {

$result = $this->Moo->request('/people/edit/1', array(
'method' => 'post',
'complete' => 'doSuccess',
'complete' => 'doComplete',
'success' => 'doSuccess',
'error' => 'doFailure',
'before' => 'doBefore',
'update' => 'update-zone'
));
$expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:doSuccess, update:"update-zone", url:"/people/edit/1"}).send();';
$expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:doComplete, onFailure:doFailure, onRequest:doBefore, onSuccess:doSuccess, update:"update-zone", url:"/people/edit/1"}).send();';
$this->assertEqual($result, $expected);
}
/**
Expand Down
33 changes: 33 additions & 0 deletions cake/tests/cases/libs/view/helpers/prototype_engine.test.php
Expand Up @@ -168,7 +168,40 @@ function testEffect() {
* @return void
**/
function testRequest() {
$result = $this->Proto->request(array('controller' => 'posts', 'action' => 'view', 1));
$expected = 'var jsRequest = new Ajax("/posts/view/1");';
$this->assertEqual($result, $expected);

$result = $this->Proto->request('/posts/view/1', array('update' => 'content'));
$expected = 'var jsRequest = new Ajax.Updater("/posts/view/1", {update:"content"});';
$this->assertEqual($result, $expected);

/* $result = $this->Proto->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);
$result = $this->Proto->request('/people/edit/1', array(
'method' => 'post',
'complete' => 'doSuccess',
'update' => '#update-zone'
));
$expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:doSuccess, update:"update-zone", url:"/people/edit/1"}).send();';
$this->assertEqual($result, $expected);
$result = $this->Proto->request('/people/edit/1', array(
'method' => 'post',
'complete' => 'doSuccess',
'update' => 'update-zone'
));
$expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:doSuccess, update:"update-zone", url:"/people/edit/1"}).send();';
$this->assertEqual($result, $expected);
*/
}
/**
* test sortable list generation
Expand Down

0 comments on commit 5f7abae

Please sign in to comment.