Skip to content

Commit

Permalink
Fixing method param in jQuery engine. Needed to be converted to 'type'.
Browse files Browse the repository at this point in the history
Fixing issues in JsHelper::submit() related to data being escaped.
Updating tests cases.
  • Loading branch information
markstory committed Jul 25, 2009
1 parent 49e0e57 commit 9a66c2c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 49 deletions.
23 changes: 16 additions & 7 deletions cake/libs/view/helpers/jquery_engine.php
Expand Up @@ -33,6 +33,7 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
'request' => array(
'type' => 'dataType',
'before' => 'beforeSend',
'method' => 'type',
),
'sortable' => array(
'complete' => 'stop',
Expand Down Expand Up @@ -180,6 +181,10 @@ function request($url, $options = array()) {
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 .'});';
}
Expand Down Expand Up @@ -241,20 +246,24 @@ function slider($options = array()) {
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;
}
}
?>
78 changes: 45 additions & 33 deletions cake/libs/view/helpers/js.php
Expand Up @@ -147,12 +147,12 @@ function call__($method, $params) {
*
* 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.
Expand Down Expand Up @@ -207,11 +207,11 @@ function getBuffer($clear = true) {
*
* ### 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.
Expand Down Expand Up @@ -257,7 +257,8 @@ function submit($caption = null, $options = array()) {
$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']);
Expand All @@ -267,6 +268,10 @@ function submit($caption = null, $options = array()) {
$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);
Expand Down Expand Up @@ -406,8 +411,8 @@ function prompt($message, $default = '') {
*
* 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.
Expand Down Expand Up @@ -624,8 +629,8 @@ function get($selector) {
*
* ### 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
Expand Down Expand Up @@ -660,16 +665,16 @@ function each($callback) {
*
* 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.
Expand All @@ -684,19 +689,21 @@ function effect($name, $options) {
*
* ### 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
Expand Down Expand Up @@ -799,7 +806,12 @@ function slider() {
* 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() {
Expand Down
12 changes: 6 additions & 6 deletions cake/tests/cases/libs/view/helpers/jquery_engine.test.php
Expand Up @@ -154,15 +154,15 @@ function testRequest() {
'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(
'update' => '#updated',
'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);
}
/**
Expand Down Expand Up @@ -240,16 +240,16 @@ function testSlider() {
**/
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);
}
}
Expand Down
17 changes: 14 additions & 3 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -355,7 +355,10 @@ function testSubmitWithMock() {
$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)
));
Expand All @@ -372,11 +375,19 @@ function testSubmitWithMock() {
$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)
));
Expand Down

0 comments on commit 9a66c2c

Please sign in to comment.