The idea is very simple, but it provides significant advantages compared to the code without using this template, it has the potential to be actively used. Strange, but I have not seen anything like this in any trend code for reuse, so I called the template "CSN-Ajax" based on the site name and the project "SKY Framewok", in which the template first appeared -
CoreSky.Net , in my project. I will describe an implementation example here based on the use of PHP, jQuery and MVC patterns. So, briefly, without comments:
1. Make a wrapper function in javascript named ajax (action, postfields, func, controller) for $ .post (..). A typical call in the application code:
ajax('edit', post_vars, function(data) {
// ....
})
2. «» jQuery $.post(..), -. controller , , «document.location.href», , (query string, GET ). .. controller , « » , .
3. , . ajax(..) GET «controller» - "__AJAX__=_"
4. index.php, :
<?php
if ('__AJAX__' == key($_GET)) {
define('AJAX', true);
array_shift($_GET);
} else {
define('AJAX', false);
}
.. javascript ajax(..), «», ajax , , . , : , layout style , . , , layout style.
5. , error handler php PHP set_error_handler(..), Exception. AJAX, , , JSON , «catch_error», :
echo json_encode(['catch_error' => "$this->errors<h1>Stdout</h1>" . html($stdout)]);
, , STDOUT PHP ob_start(). ajax(..), , JSON «catch_error», , , . , .
CSN-Ajax, -, , :
) ajax javascript, .
) ajax , layout.
) ajax.
, Framework, — , , , .
!