Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Issue #77: De-couple behavior helper from router and template
Browse files Browse the repository at this point in the history
- Refactor the keepalive helper method to use jQuery and default to window.location.url
- Add a 'url' config option to set a custom url to call
- Use head requests and do not use cache
  • Loading branch information
johanjanssens committed May 3, 2016
1 parent 943f8a3 commit 9b8048f
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions code/template/helper/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,16 @@ public function validator($config = array())
* @param array $config An optional array with configuration options
* @return string The html output
*/
public function keepalive($config = array(), TemplateInterface $template)
public function keepalive($config = array())
{
$config = new ObjectConfigJson($config);
$config->append(array(
'refresh' => 15 * 60000, //default refresh is 15min
'url' => $template->route('', false, false),
'url' => '', //default to window.location.url
));

$html = '';

// Only load once
if (!isset(self::$_loaded['keepalive']))
{
Expand All @@ -306,14 +308,29 @@ public function keepalive($config = array(), TemplateInterface $template)
$refresh = 3600000;
}

if(empty($config->url)) {
$url = 'window.location.url';
} else {
$url = "'.$config->url.'";
}

// Build the keep alive script.
//See: https://stackoverflow.com/questions/5052543/how-to-fire-ajax-request-periodically
$html =
"<script>
Kodekit.keepalive = function() {
var request = new Request({method: 'get', url: '" . $config->url . "'}).send();
}
window.addEvent('domready', function() { Kodekit.keepalive.periodical('" . $refresh . "'); });
</script>";
(function keepalive(){
kQuery(function($) {
$.ajax({
url: $url,
method: 'HEAD',
cache: false,
complete: function() {
// Schedule the next request when the current one's complete
setTimeout(keepalive, '" . $refresh . "');
}
})
});
})();</script>";

self::$_loaded['keepalive'] = true;
}
Expand Down Expand Up @@ -383,7 +400,7 @@ public function autocomplete($config = array())
'validate' => false, //Toggle if the forms validation helper is loaded
'queryVarName' => 'search',
'width' => 'resolve',
'model' => $config->model,
'model' => $config->model,
'placeholder' => $config->prompt,
'allowClear' => $config->deselect,
'value' => $config->value,
Expand All @@ -405,8 +422,7 @@ public function autocomplete($config = array())
}

$config->options->url->setQuery(array('fields' => $config->value.','.$config->text), true);

$config->options->url = (string)$config->options->url;
$config->options->url = (string) $config->options->url;

$options = $config->options;
$signature = md5('autocomplete-'.$config->element.$options);
Expand Down

0 comments on commit 9b8048f

Please sign in to comment.