我会告诉你最后删除回声,并尝试编写一些函数甚至进行测试,这样你就可以更好地理解 v3。
在这里,检查此代码,它可能会对您有所帮助,此代码已经过测试并且可以工作。
<?php
require_once ($_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php');
use Jaxon\Jaxon;
use Jaxon\Response\Response;
$jaxon = jaxon();
$jaxon->setOption('js.app.minify', TRUE);
$jaxon->setOption('js.lib.uri', '/vendor/jaxon-php/jaxon-js/dist');
/** ################################# */
/** using jaxon with just functions then you have to register each function
*
* call these functions like this:
* jaxon_demo1();
* jaxon_demo2();
*
* <button type="button" onclick="jaxon_demo1()"> call fn 1 </button>
* <button type="button" onclick="jaxon_demo2()"> call fn 2 </button>
*
*/
$jaxon->register(Jaxon::USER_FUNCTION, 'demo1');
$jaxon->register(Jaxon::USER_FUNCTION, 'demo2');
$jaxon->register(Jaxon::CALLABLE_FUNCTION, 'functionThatReturnsUsing_setReturnValue', ['mode' => "'synchronous'"]);
function demo1(){
$jaxonResponse = new Response();
$jaxonResponse->alert('Hello there, be sure to open browser console to see the console.log');
$jaxonResponse->script("console.log('Hello there')");
return $jaxonResponse;
}
function demo2(){
$jaxonResponse = new Response();
// <div id="theDiv"> this will be replaced </div>
$jaxonResponse->assign("theDiv","innerHTML",'Some content that can even be a template if you implement smarty or similar');
return $jaxonResponse;
}
/**
* check out https://github.com/jaxon-php/jaxon-js/issues/15
* this should work, take a look at the link provided
* also use the browser console to look the response data being returned
*/
function functionThatReturnsUsing_setReturnValue(){
$jaxonResponse = new Response();
// lets suppose you have a script like this
// <script>
// function demo3(){
// var myData = jaxon_functionThatReturnsUsing_setReturnValue();
// console.log(myData);
// }
// </script>
$someData = array(
'isValidated' => TRUE,
'str_data' => 'some data here',
'int_data' => 123,
'flt_data' => 1.23,
'row_data' => array(
'isValidated' => TRUE,
'str_data' => 'some data here',
'int_data' => 123,
'flt_data' => 1.23
)
);
$jaxonResponse->setReturnValue($someData);
$jaxonResponse->getOutput();
return $jaxonResponse;
}
if($jaxon->canProcessRequest()){
$jaxon->processRequest();
}
/**
* if you are using composer, then your composer.json should have this at least:
*
* {
* "require": {
* "jaxon-php/jaxon-core": "^3.2",
* "jaxon-php/jaxon-js": "^3.2"
* }
* }
*
*/
?>
<!DOCTYPE html>
<html lang="es">
<head>
<title>Demo jaxon</title>
<?php echo $jaxon->getCss(); ?>
</head>
<body>
<ul>
<li><a href="javascript:void(0)" onclick="jaxon_demo1()">demo fn 1</a></li>
<li><a href="javascript:void(0)" onclick="jaxon_demo2()">demo fn 2</a></li>
<li><a href="javascript:void(0)" onclick="demo3()">demo fn 3</a></li>
</ul>
<div id="theDiv">this will be replaced</diV>
<script>
function demo3(){
var myData = jaxon_functionThatReturnsUsing_setReturnValue();
console.log(myData);
}
</script>
<?php
echo $jaxon->getJs();
echo $jaxon->getScript();
?>
</body>
</html>