Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding querystring creation and option mapping to jquery engine.

More complex ajax requests can now be done.
  • Loading branch information...
commit e873bb5ab953b6d6b7ffdd44aae8440c0f00ed17 1 parent 7286696
Mark Story markstory authored
17 cake/libs/view/helpers/jquery_engine.php
View
@@ -28,6 +28,18 @@
class JqueryEngineHelper extends JsBaseEngineHelper {
/**
+ * Option mappings for jQuery
+ *
+ * @var array
+ **/
+ var $_optionMap = array(
+ 'request' => array(
+ 'type' => 'dataType',
+ 'complete' => 'success',
+ 'request' => 'beforeSend',
+ )
+ );
+/**
* Create javascript selector for a CSS rule
*
* @param string $selector The selector that is targeted
@@ -124,10 +136,11 @@ function request($url, $options = array()) {
$url = $this->url($url);
$options = $this->_mapOptions('request', $options);
if (isset($options['data']) && is_array($options['data'])) {
- //handle data array to query string.
+ $options['data'] = $this->_toQuerystring($options['data']);
}
$options['url'] = $url;
- $options = $this->_parseOptions($options);
+ $callbacks = array('success', 'error', 'beforeSend', 'complete');
+ $options = $this->_parseOptions($options, $callbacks);
return '$.ajax({' . $options .'});';
}
}
21 cake/libs/view/helpers/js.php
View
@@ -591,7 +591,7 @@ function _parseOptions($options, $safeKeys = array()) {
$out = array();
foreach ($options as $key => $value) {
if (!is_int($value) && !in_array($key, $safeKeys)) {
- $value = '"' . $this->escape($value) . '"';
+ $value = $this->value($value);
}
$out[] = $key . ':' . $value;
}
@@ -618,6 +618,25 @@ function _mapOptions($method, $options) {
}
return $options;
}
+/**
+ * Convert an array of data into a query string
+ *
+ * @param array $parameters Array of parameters to convert to a query string
+ * @return string Querystring fragment
+ * @access protected
+ **/
+ function _toQuerystring($parameters) {
+ $out = '';
+ $keys = array_keys($parameters);
+ $count = count($parameters);
+ for ($i = 0; $i < $count; $i++) {
+ $out .= $keys[$i] . '=' . $parameters[$keys[$i]];
+ if ($i < $count - 1) {
+ $out .= '&';
+ }
+ }
+ return $out;
+ }
}
2  cake/tests/cases/libs/view/helpers/jquery_engine.test.php
View
@@ -147,7 +147,7 @@ function testRequest() {
'type' => 'json',
'data' => array('name' => 'jim', 'height' => '185cm')
));
- $expected = '$.ajax({url:"/people/edit/1", method:"post", success:doSuccess, error:handleError, dataType:"json", data:"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);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.