Skip to content

Commit

Permalink
Starting work on Js::link()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jul 12, 2009
1 parent 4d4bc90 commit 5119e58
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cake/libs/view/helpers/js.php
Expand Up @@ -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;
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -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);
}
}


Expand Down

0 comments on commit 5119e58

Please sign in to comment.