Skip to content

Commit

Permalink
Adding event() and tests to prototype library
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 28, 2009
1 parent da2361d commit 8cc5990
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cake/libs/view/helpers/prototype_engine.php
Expand Up @@ -2,7 +2,8 @@
/**
* Prototype Engine Helper for JsHelper
*
* Provides Prototype specific Javascript for JsHelper.
* Provides Prototype specific Javascript for JsHelper. Requires at least
* Prototype 1.6
*
* PHP versions 4 and 5
*
Expand Down Expand Up @@ -67,7 +68,18 @@ function get($selector) {
* @return string completed event handler
**/
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 = "event.stop();\n" . $callback;
}
if ($options['wrap']) {
$callback = sprintf($function, $callback);
}
$out = $this->selection . ".observe(\"{$type}\", $callback);";
return $out;
}
/**
* Create a domReady event. This is a special event in many libraries
Expand Down
10 changes: 10 additions & 0 deletions cake/tests/cases/libs/view/helpers/prototype_engine.test.php
Expand Up @@ -76,7 +76,17 @@ function testSelector() {
* @return void
**/
function testEvent() {
$result = $this->Proto->get('#myLink')->event('click', 'doClick', array('wrap' => false));
$expected = '$("myLink").observe("click", doClick);';
$this->assertEqual($result, $expected);

$result = $this->Proto->get('#myLink')->event('click', 'Element.hide(this);', array('stop' => false));
$expected = '$("myLink").observe("click", function (event) {Element.hide(this);});';
$this->assertEqual($result, $expected);

$result = $this->Proto->get('#myLink')->event('click', 'Element.hide(this);');
$expected = "\$(\"myLink\").observe(\"click\", function (event) {event.stop();\nElement.hide(this);});";
$this->assertEqual($result, $expected);
}
/**
* test dom ready event creation
Expand Down

0 comments on commit 8cc5990

Please sign in to comment.