mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Adding querystring creation and option mapping to jquery engine.
More complex ajax requests can now be done.
This commit is contained in:
parent
72866966b0
commit
e873bb5ab9
3 changed files with 36 additions and 4 deletions
|
@ -27,6 +27,18 @@
|
|||
App::import('Helper', 'Js');
|
||||
|
||||
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
|
||||
*
|
||||
|
@ -124,10 +136,11 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
|
|||
$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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
$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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
}
|
||||
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 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
|
|||
'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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue