diff --git a/cake/libs/view/helpers/jquery_engine.php b/cake/libs/view/helpers/jquery_engine.php index 4ab32d5023e..5319d24c9cc 100644 --- a/cake/libs/view/helpers/jquery_engine.php +++ b/cake/libs/view/helpers/jquery_engine.php @@ -45,14 +45,26 @@ function get($selector, $multiple = false) { /** * Add an event to the script cache. Operates on the currently selected elements. * + * ### Options + * + * - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults true) + * - 'stop' - Whether you want the event to stopped. (defaults 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 - * @param boolean $wrap Whether you want your callback wrapped in ```function (event) { }``` + * @param array $options Options for the event. * @return string completed event handler **/ - function event($type, $callback, $wrap = false) { - if ($wrap) { - $callback = 'function (event) {' . $callback . '}'; + function event($type, $callback, $options = array()) { + $defaults = array('wrap' => true, 'stop' => true); + $options = array_merge($defaults, $options); + + $function = 'function (event) {%s}'; + if ($options['wrap'] && $options['stop']) { + $callback .= "\nreturn false;"; + } + if ($options['wrap']) { + $callback = sprintf($function, $callback); } $out = $this->selection . ".bind('{$type}', $callback);"; return $out; @@ -64,7 +76,7 @@ function event($type, $callback, $wrap = false) { * @return string completed domReady method **/ function domReady($functionBody) { - return $this->get('document')->event('ready', $functionBody, true); + return $this->get('document')->event('ready', $functionBody, array('stop' => false)); } /** * Create an iteration over the current selection result. diff --git a/cake/libs/view/helpers/js.php b/cake/libs/view/helpers/js.php index f450d58228a..da11a623198 100644 --- a/cake/libs/view/helpers/js.php +++ b/cake/libs/view/helpers/js.php @@ -497,12 +497,17 @@ function get($selector, $multiple = false) { /** * Add an event to the script cache. Operates on the currently selected elements. * + * ### 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) + * * @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 - * @param boolean $wrap Whether you want your callback wrapped in ```function (event) { }``` + * @param array $options Options for the event. * @return string completed event handler **/ - function event($type, $callback, $wrap = false) { + function event($type, $callback, $options = array()) { trigger_error(sprintf(__('%s does not have event() implemented', true), get_class($this)), E_USER_WARNING); } /** diff --git a/cake/tests/cases/libs/view/helpers/jquery_engine.test.php b/cake/tests/cases/libs/view/helpers/jquery_engine.test.php index d36cba87a37..f5fc5d99e87 100644 --- a/cake/tests/cases/libs/view/helpers/jquery_engine.test.php +++ b/cake/tests/cases/libs/view/helpers/jquery_engine.test.php @@ -68,12 +68,16 @@ function testSelector() { * @return void **/ function testEvent() { - $result = $this->Jquery->get('#myLink')->event('click', 'doClick'); + $result = $this->Jquery->get('#myLink')->event('click', 'doClick', array('wrap' => false)); $expected = "$('#myLink').bind('click', doClick);"; $this->assertEqual($result, $expected); - $result = $this->Jquery->get('#myLink')->event('click', '$(this).hide();', true); - $expected = "\$('#myLink').bind('click', function (event) {\$(this).hide();});"; + $result = $this->Jquery->get('#myLink')->event('click', '$(this).show();', array('stop' => false)); + $expected = "$('#myLink').bind('click', function (event) {\$(this).show();});"; + $this->assertEqual($result, $expected); + + $result = $this->Jquery->get('#myLink')->event('click', '$(this).hide();'); + $expected = "\$('#myLink').bind('click', function (event) {\$(this).hide();\nreturn false;});"; $this->assertEqual($result, $expected); } /**