Fixing method param in jQuery engine. Needed to be converted to 'type'.

Fixing issues in JsHelper::submit() related to data being escaped.
Updating tests cases.
This commit is contained in:
mark_story 2009-07-24 23:29:44 -04:00
parent 49e0e5743f
commit 9a66c2cfc5
4 changed files with 81 additions and 49 deletions

View file

@ -33,6 +33,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
'request' => array(
'type' => 'dataType',
'before' => 'beforeSend',
'method' => 'type',
),
'sortable' => array(
'complete' => 'stop',
@ -180,6 +181,10 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
unset($options['update']);
}
$callbacks = array('success', 'error', 'beforeSend', 'complete');
if (isset($options['dataExpression'])) {
$callbacks[] = 'data';
unset($options['dataExpression']);
}
$options = $this->_parseOptions($options, $callbacks);
return '$.ajax({' . $options .'});';
}
@ -241,20 +246,24 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
return $this->_methodTemplate('slider', $template, $options, $callbacks);
}
/**
* Serialize the form attached to $selector. If the current selection is not an input or
* Serialize a form attached to $selector. If the current selection is not an input or
* form, errors will be created in the Javascript.
*
* Pass `true` for $isForm if the current selection is a form element.
*
* @param boolean $isForm is the current selection a form?
* @param array $options Options for the serialization
* @return string completed form serialization script
* @see JsHelper::serializeForm() for option list.
**/
function serializeForm($isForm = false) {
function serializeForm($options = array()) {
$options = array_merge(array('isForm' => false, 'inline' => false), $options);
$selector = $this->selection;
if (!$isForm) {
if (!$options['isForm']) {
$selector = $this->selection . '.closest("form")';
}
return $selector . '.serialize();';
$method = '.serialize()';
if (!$options['inline']) {
$method .= ';';
}
return $selector . $method;
}
}
?>

View file

@ -147,12 +147,12 @@ class JsHelper extends AppHelper {
*
* Options
*
* - 'inline' - Set to true to have scripts output as a script block inline
* if 'cache' is also true, a script link tag will be generated. (default true)
* - 'cache' - Set to true to have scripts cached to a file and linked in (default false)
* - 'clear' - Set to false to prevent script cache from being cleared (default true)
* - 'onDomReady' - wrap cached scripts in domready event (default true)
* - 'safe' - if an inline block is generated should it be wrapped in <![CDATA[ ... ]]> (default true)
* - `inline` - Set to true to have scripts output as a script block inline
* if `cache` is also true, a script link tag will be generated. (default true)
* - `cache` - Set to true to have scripts cached to a file and linked in (default false)
* - `clear` - Set to false to prevent script cache from being cleared (default true)
* - `onDomReady` - wrap cached scripts in domready event (default true)
* - `safe` - if an inline block is generated should it be wrapped in <![CDATA[ ... ]]> (default true)
*
* @param array $options options for the code block
* @return string completed javascript tag.
@ -207,11 +207,11 @@ class JsHelper extends AppHelper {
*
* ### Options
*
* - confirm - Generate a confirm() dialog before sending the event.
* - id - use a custom id.
* - htmlAttrs - additional non-standard htmlAttributes. Standard attributes are class, id,
* - `confirm` - Generate a confirm() dialog before sending the event.
* - `id` - use a custom id.
* - `htmlAttributes` - additional non-standard htmlAttributes. Standard attributes are class, id,
* rel, title, escape, onblur and onfocus.
* - buffer - Disable the buffering and return a script tag in addition to the link.
* - `buffer` - Disable the buffering and return a script tag in addition to the link.
*
* @param string $title Title for the link.
* @param mixed $url Mixed either a string URL or an cake url array.
@ -257,7 +257,8 @@ class JsHelper extends AppHelper {
$out = $this->Form->submit($caption, $htmlOptions);
$this->get('#' . $htmlOptions['id']);
$options['data'] = $this->serializeForm(false);
$options['data'] = $this->serializeForm(array('isForm' => false, 'inline' => true));
$requestString = $url = '';
if (isset($options['confirm'])) {
$requestString = $this->confirmReturn($options['confirm']);
@ -267,6 +268,10 @@ class JsHelper extends AppHelper {
$url = $options['url'];
unset($options['url']);
}
if (!isset($options['method'])) {
$options['method'] = 'POST';
}
$options['dataExpression'] = true;
$requestString .= $this->request($url, $options);
if (!empty($requestString)) {
$event = $this->event('click', $requestString, $options);
@ -406,8 +411,8 @@ class JsBaseEngineHelper extends AppHelper {
*
* Options:
*
* - 'prefix' - String prepended to the returned data.
* - 'postfix' - String appended to the returned data.
* - `prefix` - String prepended to the returned data.
* - `postfix` - String appended to the returned data.
*
* @param array $data Data to be converted.
* @param array $options Set of options, see above.
@ -624,8 +629,8 @@ class JsBaseEngineHelper extends AppHelper {
*
* ### Options
*
* - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults to true)
* - 'stop' - Whether you want the event to stopped. (defaults to true)
* - `wrap` - Whether you want the callback wrapped in an anonymous function. (defaults to true)
* - `stop` - Whether you want the event to stopped. (defaults to true)
*
* @param string $type Type of event to bind to the current dom id
* @param string $callback The Javascript function you wish to trigger or the function literal
@ -660,16 +665,16 @@ class JsBaseEngineHelper extends AppHelper {
*
* The following effects are supported by all JsEngines
*
* - 'show' - reveal an element.
* - 'hide' - hide an element.
* - 'fadeIn' - Fade in an element.
* - 'fadeOut' - Fade out an element.
* - 'slideIn' - Slide an element in.
* - 'slideOut' - Slide an element out.
* - `show` - reveal an element.
* - `hide` - hide an element.
* - `fadeIn` - Fade in an element.
* - `fadeOut` - Fade out an element.
* - `slideIn` - Slide an element in.
* - `slideOut` - Slide an element out.
*
* ### Options
*
* - 'speed' - Speed at which the animation should occur. Accepted values are 'slow', 'fast'. Not all effects use
* - `speed` - Speed at which the animation should occur. Accepted values are 'slow', 'fast'. Not all effects use
* the speed option.
*
* @param string $name The name of the effect to trigger.
@ -684,19 +689,21 @@ class JsBaseEngineHelper extends AppHelper {
*
* ### Event Options
*
* - 'complete' - Callback to fire on complete.
* - 'success' - Callback to fire on success.
* - 'before' - Callback to fire on request initialization.
* - 'error' - Callback to fire on request failure.
* - `complete` - Callback to fire on complete.
* - `success` - Callback to fire on success.
* - `before` - Callback to fire on request initialization.
* - `error` - Callback to fire on request failure.
*
* ### Options
*
* - 'method' - The method to make the request with defaults to GET in more libraries
* - 'async' - Whether or not you want an asynchronous request.
* - 'data' - Additional data to send.
* - 'update' - Dom id to update with the content of the request.
* - 'type' - Data type for response. 'json' and 'html' are supported. Default is html for most libraries.
* - 'evalScripts' - Whether or not <script> tags should be eval'ed.
* - `method` - The method to make the request with defaults to GET in more libraries
* - `async` - Whether or not you want an asynchronous request.
* - `data` - Additional data to send.
* - `update` - Dom id to update with the content of the request.
* - `type` - Data type for response. 'json' and 'html' are supported. Default is html for most libraries.
* - `evalScripts` - Whether or not <script> tags should be eval'ed.
* - `dataExpression` - Should the `data` key be treated as a callback. Useful for supplying `$options['data']` as
* another Javascript expression.
*
* @param mixed $url Array or String URL to target with the request.
* @param array $options Array of options. See above for cross library supported options
@ -799,7 +806,12 @@ class JsBaseEngineHelper extends AppHelper {
* Converts the form or the form element attached to the current selection into a string/json object
* (depending on the library implementation) for use with XHR operations.
*
* @param boolean $isForm is the current selection a form?
* ### Options
*
* - isForm - is the current selection a form, or an input? (defaults to false)
* - inline - is the rendered statement going to be used inside another JS statement? (defaults to false)
*
* @param array $options options for serialization generation.
* @return string completed form serialization script
**/
function serializeForm() {

View file

@ -154,7 +154,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
'type' => 'json',
'data' => array('name' => 'jim', 'height' => '185cm')
));
$expected = '$.ajax({beforeSend:doBefore, complete:doComplete, 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, success:doSuccess, type:"post", url:"\\/people\\/edit\\/1"});';
$this->assertEqual($result, $expected);
$result = $this->Jquery->request('/people/edit/1', array(
@ -162,7 +162,7 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
'success' => 'doFoo',
'method' => 'post'
));
$expected = '$.ajax({method:"post", success:function (msg, status) {$("#updated").html(msg);}, url:"\\/people\\/edit\\/1"});';
$expected = '$.ajax({success:function (msg, status) {$("#updated").html(msg);}, type:"post", url:"\\/people\\/edit\\/1"});';
$this->assertEqual($result, $expected);
}
/**
@ -240,16 +240,16 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
**/
function testSerializeForm() {
$this->Jquery->get('#element');
$result = $this->Jquery->serializeForm(false);
$result = $this->Jquery->serializeForm(array('isForm' => false));
$expected = '$("#element").closest("form").serialize();';
$this->assertEqual($result, $expected);
$result = $this->Jquery->serializeForm(true);
$result = $this->Jquery->serializeForm(array('isForm' => true));
$expected = '$("#element").serialize();';
$this->assertEqual($result, $expected);
$result = $this->Jquery->serializeForm();
$expected = '$("#element").closest("form").serialize();';
$result = $this->Jquery->serializeForm(array('isForm' => false, 'inline' => true));
$expected = '$("#element").closest("form").serialize()';
$this->assertEqual($result, $expected);
}
}

View file

@ -355,7 +355,10 @@ CODE;
$this->Js->TestJsEngine->expectAt(1, 'dispatchMethod', array('serializeForm', '*'));
$this->Js->TestJsEngine->expectAt(2, 'dispatchMethod', array('request', '*'));
$params = array('update' => $options['update'], 'data' => 'serialize-code');
$params = array(
'update' => $options['update'], 'data' => 'serialize-code',
'method' => 'POST', 'dataExpression' => true
);
$this->Js->TestJsEngine->expectAt(3, 'dispatchMethod', array(
'event', array('click', "ajax-code", $params)
));
@ -372,11 +375,19 @@ CODE;
$this->Js->TestJsEngine->expectAt(4, 'dispatchMethod', array('get', '*'));
$this->Js->TestJsEngine->expectAt(5, 'dispatchMethod', array('serializeForm', '*'));
$requestParams = array(
'/custom/url', array('update' => '#content', 'data' => 'serialize-code')
'/custom/url', array(
'update' => '#content',
'data' => 'serialize-code',
'method' => 'POST',
'dataExpression' => true
)
);
$this->Js->TestJsEngine->expectAt(6, 'dispatchMethod', array('request', $requestParams));
$params = array('update' => '#content', 'data' => 'serialize-code');
$params = array(
'update' => '#content', 'data' => 'serialize-code',
'method' => 'POST', 'dataExpression' => true
);
$this->Js->TestJsEngine->expectAt(7, 'dispatchMethod', array(
'event', array('click', "ajax-code", $params)
));