Skip to content

Instantly share code, notes, and snippets.

@funyx funyx/jsAjax.php
Created Aug 26, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.