Permalink
Browse files

Adding querystring creation and option mapping to jquery engine.

More complex ajax requests can now be done.
  • Loading branch information...
1 parent 7286696 commit e873bb5ab953b6d6b7ffdd44aae8440c0f00ed17 @markstory markstory committed Mar 15, 2009
@@ -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 .'});';
}
}
@@ -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;
+ }
}
@@ -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);
}
}

0 comments on commit e873bb5

Please sign in to comment.