From 5f7abae48c59475de7dbb19cd9315e51e74d3970 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 5 Apr 2009 23:49:09 -0400 Subject: [PATCH] Fixing request() callbacks. Starting test for prototype --- cake/libs/view/helpers/jquery_engine.php | 3 +- cake/libs/view/helpers/js.php | 3 +- cake/libs/view/helpers/mootools_engine.php | 5 +-- cake/libs/view/helpers/prototype_engine.php | 35 +++++++++++++++++-- .../libs/view/helpers/jquery_engine.test.php | 6 ++-- .../view/helpers/mootools_engine.test.php | 7 ++-- .../view/helpers/prototype_engine.test.php | 33 +++++++++++++++++ 7 files changed, 81 insertions(+), 11 deletions(-) diff --git a/cake/libs/view/helpers/jquery_engine.php b/cake/libs/view/helpers/jquery_engine.php index 70b6c41ab..14e82cb9a 100644 --- a/cake/libs/view/helpers/jquery_engine.php +++ b/cake/libs/view/helpers/jquery_engine.php @@ -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', diff --git a/cake/libs/view/helpers/js.php b/cake/libs/view/helpers/js.php index 108057bb5..b8c2e6d2d 100644 --- a/cake/libs/view/helpers/js.php +++ b/cake/libs/view/helpers/js.php @@ -562,7 +562,8 @@ class JsBaseEngineHelper extends AppHelper { * ### 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 diff --git a/cake/libs/view/helpers/mootools_engine.php b/cake/libs/view/helpers/mootools_engine.php index f4efaef5a..45d3427fa 100644 --- a/cake/libs/view/helpers/mootools_engine.php +++ b/cake/libs/view/helpers/mootools_engine.php @@ -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( @@ -185,7 +186,7 @@ class MootoolsEngineHelper extends JsBaseEngineHelper { 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);"; } diff --git a/cake/libs/view/helpers/prototype_engine.php b/cake/libs/view/helpers/prototype_engine.php index 48f543afb..dd6112393 100644 --- a/cake/libs/view/helpers/prototype_engine.php +++ b/cake/libs/view/helpers/prototype_engine.php @@ -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 @@ -153,7 +159,32 @@ class PrototypeEngineHelper extends JsBaseEngineHelper { * @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. diff --git a/cake/tests/cases/libs/view/helpers/jquery_engine.test.php b/cake/tests/cases/libs/view/helpers/jquery_engine.test.php index 81bfc964f..b967fcc80 100644 --- a/cake/tests/cases/libs/view/helpers/jquery_engine.test.php +++ b/cake/tests/cases/libs/view/helpers/jquery_engine.test.php @@ -146,12 +146,14 @@ class JqueryEngineHelperTestCase extends CakeTestCase { $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( diff --git a/cake/tests/cases/libs/view/helpers/mootools_engine.test.php b/cake/tests/cases/libs/view/helpers/mootools_engine.test.php index 780ade21f..a5ffd3437 100644 --- a/cake/tests/cases/libs/view/helpers/mootools_engine.test.php +++ b/cake/tests/cases/libs/view/helpers/mootools_engine.test.php @@ -181,10 +181,13 @@ class MooEngineHelperTestCase extends CakeTestCase { $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); } /** diff --git a/cake/tests/cases/libs/view/helpers/prototype_engine.test.php b/cake/tests/cases/libs/view/helpers/prototype_engine.test.php index 92d10a116..b5ad20fcc 100644 --- a/cake/tests/cases/libs/view/helpers/prototype_engine.test.php +++ b/cake/tests/cases/libs/view/helpers/prototype_engine.test.php @@ -168,7 +168,40 @@ class PrototypeEngineHelperTestCase extends CakeTestCase { * @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