mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Refactoring event() and adding ability to stop default events.
This commit is contained in:
parent
ae2114dcc1
commit
ffdbec12fe
3 changed files with 31 additions and 10 deletions
|
@ -45,14 +45,26 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
|
|||
/**
|
||||
* 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 @@ class JqueryEngineHelper extends JsBaseEngineHelper {
|
|||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
/**
|
||||
* 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 @@ class JqueryEngineHelperTestCase extends CakeTestCase {
|
|||
* @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);
|
||||
}
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue