Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updating Js::link() adding confirm option.

  • Loading branch information...
commit 04fa46f228bf1634c815eb7ffa7e981ba40c16b8 1 parent 5119e58
@markstory markstory authored
View
22 cake/libs/view/helpers/js.php
@@ -206,8 +206,12 @@ function getBuffer($clear = true) {
/**
* Generate an 'Ajax' link. Uses the selected JS engine to create a link
* element that is enhanced with Javascript. Options can include
- * both those for HtmlHelper::link() and JsBaseEngine::request()
+ * both those for HtmlHelper::link() and JsBaseEngine::request(), JsBaseEngine::event();
*
+ * ### Options
+ *
+ * - confirm - Generate a confirm() dialog before sending the event.
+ *
* @param string $title Title for the link.
* @param mixed $url Mixed either a string URL or an cake url array.
* @param array $options Options for both the HTML element and Js::request()
@@ -219,26 +223,34 @@ function link($title, $url = null, $options = array()) {
}
$htmlOptions = $this->_getHtmlOptions($options);
$out = $this->Html->link($title, $url, $htmlOptions);
- $this->get('#' . $options['id']);
- $requestString = $this->request($url, $options);
+ $this->get('#' . $htmlOptions['id']);
+ $requestString = '';
+ if (isset($options['confirm'])) {
+ $requestString .= 'var _confirm = ' . $this->confirm($options['confirm']);
+ $requestString .= "if (!_confirm) {\n\treturn false;\n}";
+ unset($options['confirm']);
+ }
+ $requestString .= $this->request($url, $options);
if (!empty($requestString)) {
-
+ $this->event('click', $requestString, $options);
}
return $out;
}
/**
* Parse a set of Options and extract the Html options.
+ * Extracted Html Options are removed from the $options param.
*
* @param array Options to filter.
* @return array Array of options for non-js.
**/
- function _getHtmlOptions($options) {
+ function _getHtmlOptions(&$options) {
$htmlKeys = array('class', 'id', 'escape', 'onblur', 'onfocus', 'rel', 'title');
$htmlOptions = array();
foreach ($htmlKeys as $key) {
if (isset($options[$key])) {
$htmlOptions[$key] = $options[$key];
}
+ unset($options[$key]);
}
return $htmlOptions;
}
View
70 cake/tests/cases/libs/view/helpers/js.test.php
@@ -83,7 +83,7 @@ class JsHelperTestCase extends CakeTestCase {
*/
function startTest() {
$this->Js =& new JsHelper('JsBase');
- $this->Js->Html =& new HtmlHelper();
+ $this->Js->Html =& new HtmlHelper();
$this->Js->JsBaseEngine =& new JsBaseEngineHelper();
$view =& new JsHelperMockView();
@@ -100,6 +100,16 @@ function endTest() {
unset($this->Js);
}
/**
+ * Switches $this->Js to a mocked engine.
+ *
+ * @return void
+ **/
+ function _useMock() {
+ $this->Js =& new JsHelper(array('TestJs'));
+ $this->Js->TestJsEngine =& new TestJsEngineHelper();
+ $this->Js->Html =& new HtmlHelper();
+ }
+/**
* test object construction
*
* @return void
@@ -123,15 +133,14 @@ function testConstruction() {
* @return void
**/
function testMethodDispatching() {
- $js =& new JsHelper(array('TestJs'));
- $js->TestJsEngine =& new TestJsEngineHelper();
- $js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));
+ $this->_useMock();
+ $this->Js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));
- $js->methodOne();
+ $this->Js->methodOne();
- $js->TestEngine =& new StdClass();
+ $this->Js->TestEngine =& new StdClass();
$this->expectError();
- $js->someMethodThatSurelyDoesntExist();
+ $this->Js->someMethodThatSurelyDoesntExist();
}
/**
* Test that method dispatching respects buffer parameters and bufferedMethods Lists.
@@ -139,47 +148,47 @@ function testMethodDispatching() {
* @return void
**/
function testMethodDispatchWithBuffering() {
- $js =& new JsHelper(array('TestJs'));
- $js->TestJsEngine = new TestJsEngineHelper();
- $js->TestJsEngine->bufferedMethods = array('event', 'sortables');
- $js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*'));
+ $this->_useMock();
+
+ $this->Js->TestJsEngine->bufferedMethods = array('event', 'sortables');
+ $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'This is an event call', array('event', '*'));
- $js->event('click', 'foo');
- $result = $js->getBuffer();
+ $this->Js->event('click', 'foo');
+ $result = $this->Js->getBuffer();
$this->assertEqual(count($result), 1);
$this->assertEqual($result[0], 'This is an event call');
- $result = $js->event('click', 'foo', array('buffer' => false));
- $buffer = $js->getBuffer();
+ $result = $this->Js->event('click', 'foo', array('buffer' => false));
+ $buffer = $this->Js->getBuffer();
$this->assertTrue(empty($buffer));
$this->assertEqual($result, 'This is an event call');
- $result = $js->event('click', 'foo', false);
- $buffer = $js->getBuffer();
+ $result = $this->Js->event('click', 'foo', false);
+ $buffer = $this->Js->getBuffer();
$this->assertTrue(empty($buffer));
$this->assertEqual($result, 'This is an event call');
- $js->TestJsEngine->setReturnValue('dispatchMethod', 'I am not buffered.', array('effect', '*'));
+ $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'I am not buffered.', array('effect', '*'));
- $result = $js->effect('slideIn');
- $buffer = $js->getBuffer();
+ $result = $this->Js->effect('slideIn');
+ $buffer = $this->Js->getBuffer();
$this->assertTrue(empty($buffer));
$this->assertEqual($result, 'I am not buffered.');
- $result = $js->effect('slideIn', true);
- $buffer = $js->getBuffer();
+ $result = $this->Js->effect('slideIn', true);
+ $buffer = $this->Js->getBuffer();
$this->assertNull($result);
$this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.');
- $result = $js->effect('slideIn', array('speed' => 'slow'), true);
- $buffer = $js->getBuffer();
+ $result = $this->Js->effect('slideIn', array('speed' => 'slow'), true);
+ $buffer = $this->Js->getBuffer();
$this->assertNull($result);
$this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.');
- $result = $js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
- $buffer = $js->getBuffer();
+ $result = $this->Js->effect('slideIn', array('speed' => 'slow', 'buffer' => true));
+ $buffer = $this->Js->getBuffer();
$this->assertNull($result);
$this->assertEqual(count($buffer), 1);
$this->assertEqual($buffer[0], 'I am not buffered.');
@@ -190,7 +199,7 @@ function testMethodDispatchWithBuffering() {
* @return void
**/
function testWriteScriptsNoFile() {
- $this->Js->JsBaseEngine = new TestJsEngineHelper();
+ $this->_useMock();
$this->Js->buffer('one = 1;');
$this->Js->buffer('two = 2;');
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'cache' => false));
@@ -203,7 +212,7 @@ function testWriteScriptsNoFile() {
);
$this->assertTags($result, $expected, true);
- $this->Js->JsBaseEngine->expectAtLeastOnce('domReady');
+ $this->Js->TestJsEngine->expectAtLeastOnce('domReady');
$result = $this->Js->writeBuffer(array('onDomReady' => true, 'cache' => false));
$view =& new JsHelperMockView();
@@ -240,6 +249,11 @@ function testWriteScriptsInFile() {
* @return void
**/
function testLink() {
+ $this->_useMock();
+ $this->Js->TestJsEngine->setReturnValue('request', 'ajax code');
+ $this->Js->TestJsEngine->expectAt(0, 'request', array('/posts/view/1', '*'));
+ $this->Js->TestJsEngine->expectAt(0, 'event', array('click', 'ajax code'));
+
$result = $this->Js->link('test link', '/posts/view/1', array('update' => '#content'));
$expected = array(
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
Please sign in to comment.
Something went wrong with that request. Please try again.