Skip to content
Permalink
Browse files

Refactoring event() and adding ability to stop default events.

  • Loading branch information...
markstory committed Mar 15, 2009
1 parent ae2114d commit ffdbec12fea1b0bb758ef54a3be797e5420da0a7
@@ -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.
@@ -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);
}
/**
@@ -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);
}
/**

0 comments on commit ffdbec1

Please sign in to comment.
You can’t perform that action at this time.