Fixing request() callbacks. Starting test for prototype

This commit is contained in:
mark_story 2009-04-05 23:49:09 -04:00
parent 87c5a5da6a
commit 5f7abae48c
7 changed files with 81 additions and 11 deletions

View file

@ -35,8 +35,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
var $_optionMap = array( var $_optionMap = array(
'request' => array( 'request' => array(
'type' => 'dataType', 'type' => 'dataType',
'complete' => 'success', 'before' => 'beforeSend',
'request' => 'beforeSend',
), ),
'sortable' => array( 'sortable' => array(
'complete' => 'stop', 'complete' => 'stop',

View file

@ -562,7 +562,8 @@ class JsBaseEngineHelper extends AppHelper {
* ### Event Options * ### Event Options
* *
* - 'complete' - Callback to fire on complete. * - '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. * - 'error' - Callback to fire on request failure.
* *
* ### Options * ### Options

View file

@ -38,7 +38,8 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
var $_optionMap = array( var $_optionMap = array(
'request' => array( 'request' => array(
'complete' => 'onComplete', 'complete' => 'onComplete',
'request' => 'onRequest', 'success' => 'onSuccess',
'before' => 'onRequest',
'error' => 'onFailure' 'error' => 'onFailure'
), ),
'sortable' => array( 'sortable' => array(
@ -185,7 +186,7 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
unset($options['type']); unset($options['type']);
} }
$options['url'] = $url; $options['url'] = $url;
$callbacks = array('onComplete', 'onFailure', 'onRequest'); $callbacks = array('onComplete', 'onFailure', 'onRequest', 'onSuccess', 'onCancel', 'onException');
$options = $this->_parseOptions($options, $callbacks); $options = $this->_parseOptions($options, $callbacks);
return "var jsRequest = new Request$type({{$options}}).send($data);"; return "var jsRequest = new Request$type({{$options}}).send($data);";
} }

View file

@ -34,7 +34,13 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
* @var array * @var array
**/ **/
var $_optionMap = array( var $_optionMap = array(
'request' => array(
'async' => 'asyncrhronous',
'data' => 'parameters',
'before' => 'onCreate',
'complete' => 'onSuccess',
'error' => 'onFailure'
)
); );
/** /**
* Create javascript selector for a CSS rule * Create javascript selector for a CSS rule
@ -153,7 +159,32 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
* @return string The completed ajax call. * @return string The completed ajax call.
**/ **/
function request($url, $options = array()) { 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. * Create a sortable element.

View file

@ -146,12 +146,14 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
$result = $this->Jquery->request('/people/edit/1', array( $result = $this->Jquery->request('/people/edit/1', array(
'method' => 'post', 'method' => 'post',
'complete' => 'doSuccess', 'before' => 'doBefore',
'complete' => 'doComplete',
'success' => 'doSuccess',
'error' => 'handleError', 'error' => 'handleError',
'type' => 'json', 'type' => 'json',
'data' => array('name' => 'jim', 'height' => '185cm') '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); $this->assertEqual($result, $expected);
$result = $this->Jquery->request('/people/edit/1', array( $result = $this->Jquery->request('/people/edit/1', array(

View file

@ -181,10 +181,13 @@ class MooEngineHelperTestCase extends CakeTestCase {
$result = $this->Moo->request('/people/edit/1', array( $result = $this->Moo->request('/people/edit/1', array(
'method' => 'post', 'method' => 'post',
'complete' => 'doSuccess', 'complete' => 'doComplete',
'success' => 'doSuccess',
'error' => 'doFailure',
'before' => 'doBefore',
'update' => 'update-zone' '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); $this->assertEqual($result, $expected);
} }
/** /**

View file

@ -168,7 +168,40 @@ class PrototypeEngineHelperTestCase extends CakeTestCase {
* @return void * @return void
**/ **/
function testRequest() { 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 * test sortable list generation