Skip to content

Commit

Permalink
Refactoring event() and adding ability to stop default events.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 15, 2009
1 parent ae2114d commit ffdbec1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
22 changes: 17 additions & 5 deletions cake/libs/view/helpers/jquery_engine.php
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions cake/libs/view/helpers/js.php
Expand Up @@ -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);
}
/**
Expand Down
10 changes: 7 additions & 3 deletions cake/tests/cases/libs/view/helpers/jquery_engine.test.php
Expand Up @@ -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);
}
/**
Expand Down

0 comments on commit ffdbec1

Please sign in to comment.