Created
August 26, 2019 08:06
-
-
Save funyx/1a59d57d537e72c915ecb56426e73400 to your computer and use it in GitHub Desktop.
atk4\ui\jsAjax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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