Skip to content

Instantly share code, notes, and snippets.

@funyx
Created August 26, 2019 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funyx/1a59d57d537e72c915ecb56426e73400 to your computer and use it in GitHub Desktop.
Save funyx/1a59d57d537e72c915ecb56426e73400 to your computer and use it in GitHub Desktop.
atk4\ui\jsAjax
<?php
namespace atk4\ui;
use atk4\ui\Exception;
class jsAjax implements \atk4\ui\jsExpressionable
{
public $setup = null;
public $available_options = [
'accepts',
'async',
'beforeSend',
'cache',
'complete',
'contents',
'context',
'converters',
'crossDomain',
'data',
'dataFilter',
'dataType',
'error',
'global',
'headers',
'ifModified',
'isLocal',
'jsonp',
'jsonpCallback',
'method',
'mimeType',
'password',
'processData',
'scriptAttrs',
'scriptCharset',
'statusCode',
'success',
'timeout',
'traditional',
'type',
'url',
'username',
'xhr',
'xhrFields'
];
public function __construct($setup = null)
{
if (is_null($setup)) {
throw new Exception('Ajax not set up! Pass ajax options parameters.');
}
foreach ($setup as $key => $value) {
if (in_array($key, $this->available_options)) {
$this->setup[$key] = $value;
}
}
}
public function jsRender()
{
$output = '';
$output .= '$.ajax({';
foreach ($this->setup as $param => $value) {
if (is_string($value)) {
$value = "'$value'";
}
if (is_a($value, 'atk4\ui\jsExpression')) {
$value = $value->jsRender();
}
if (is_a($value, 'atk4\ui\jsFunction')) {
$value = $value->jsRender();
}
$output .= "\n$param : $value,";
}
$output .= '})';
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment