Permalink
Browse files

Adding request() to mootools.

  • Loading branch information...
1 parent e237eec commit 91903a4dec9b3655d72bb9525e9cdf6405b56e4a @markstory markstory committed Mar 16, 2009
@@ -37,7 +37,6 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
**/
var $_optionMap = array(
'request' => array(
- 'type' => 'dataType',
'complete' => 'onComplete',
'request' => 'onRequest',
'error' => 'onFailure'
@@ -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);";
}
}
?>
@@ -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.