Skip to content
Permalink
Browse files

Starting work on Js::link()

  • Loading branch information...
markstory committed Jul 12, 2009
1 parent 4d4bc90 commit 5119e58e3ba6dd79e451ebf1278626e3dcd61577
Showing with 53 additions and 0 deletions.
  1. +39 −0 cake/libs/view/helpers/js.php
  2. +14 −0 cake/tests/cases/libs/view/helpers/js.test.php
@@ -203,6 +203,45 @@ function getBuffer($clear = true) {
}
return $scripts;
}
/**
* 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()
*
* @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()
* @return string Completed link. If buffering is disabled a script tag will be returned as well.
**/
function link($title, $url = null, $options = array()) {
if (!isset($options['id'])) {
$options['id'] = 'link-' . intval(mt_rand());
}
$htmlOptions = $this->_getHtmlOptions($options);
$out = $this->Html->link($title, $url, $htmlOptions);
$this->get('#' . $options['id']);
$requestString = $this->request($url, $options);
if (!empty($requestString)) {
}
return $out;
}
/**
* Parse a set of Options and extract the Html options.
*
* @param array Options to filter.
* @return array Array of options for non-js.
**/
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];
}
}
return $htmlOptions;
}
}
/**
@@ -234,6 +234,20 @@ function testWriteScriptsInFile() {
@unlink(WWW_ROOT . $filename[1]);
}
/**
* test link()
*
* @return void
**/
function testLink() {
$result = $this->Js->link('test link', '/posts/view/1', array('update' => '#content'));
$expected = array(
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
'test link',
'/a'
);
$this->assertTags($result, $expected);
}
}

0 comments on commit 5119e58

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