十分钟学会 xajax

2016-02-19 20:37 4 1 收藏

下面图老师小编跟大家分享十分钟学会 xajax,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~

【 tulaoshi.com - Web开发 】

译者按: xajax 最大的特点是他采用了xml response,这样我们可以用php来布置,处理异步传送数据之后,网页内容的更新。而这些操作其它的ajax 框架都是由js来完成的的。xajax 使我们只需要写一些php函数,就可以实现。
所有学好xajax的关健在于熟练掌握 xajaxresponse 类。

tutorials:learn xajax in 10 minutes
教程:十分钟学会 xajax

using xajax in a php script
一个使用的xajax的php脚本:

include the xajax class library:
调用xajax类库:

require_once("xajax.inc.php");

instantiate the xajax object:
实例化xajax对象

$xajax = new xajax();

register the names of the php functions you want to be able to call through xajax:
注册一个你想用xajax来调用的php函数名(与javascript中的函数名相对应 xajax_myfunction)

$xajax-registerfunction("myfunction");

write the php functions you have registered and use the xajaxresponse object to return xml commands from them:
编写那个你刚刚已经注册的php函数,并从中用 xajaxresponse 对象来返回xml指令集

function myfunction($arg)
{
    // do some stuff based on $arg like query data from a database and
    // put it into a variable like $newcontent
    //对参数$arg做一些诸如:从数据库中获取数据后定义给$newcontent 变量的基本操作
   
    // instantiate the xajaxresponse object
    //实例化 xajaxresponse 对象
    $objresponse = new xajaxresponse();
   
    // add a command to the response to assign the innerhtml attribute of
    // the element with id="someelementid" to whatever the new content is
    // 在响应实例中添加一个命令,用来将id为someelementid的innerhtml元素属性
    // 变为任何新的内容.
    $objresponse-addassign("someelementid","innerhtml", $newcontent);
   
    //return the xml response generated by the xajaxresponse object
    //返回由 xajaxresponse 对象所生成的xml 响应
    return $objresponse-getxml();
}

before your script sends any output, have xajax handle any requests:
在你脚本传送出任何东西前,xajax都要处理所有请求

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

$xajax-processrequests();

between your head/head tags, tell xajax to generate the necessary javascript:
在该页的head和/head标签之间插入下列代码,使xajax实例可以自己生成所必需的js

?php $xajax-printjavascript(); ?

call the function from a javascript event or function in your application:

从你程序中的js 事件或函数调用之前你已经注册过的相对应函数

div id="someelementid"/div
button onclick="xajax_myfunction(someargument);"

that's it. xajax takes care of most everything else. your biggest task is writing the php functions and returning xajax xml responses from them-- which is made extremely easy by the xajaxresponse class.
只需这些步骤。其他的交由xajax 去处理吧。你最主要的任务只是编写php中的函数,只要使它们能返回xajax的xml响应就行了,而这步可以用xajaxresponse 类轻松解决。

how do i update my content asynchronously?
如何异步更新我的内容?

perhaps the most unique feature of xajax is the xajaxresponse class. other ajax libraries require you to write your own callback handlers in javascript to process the data returned from an asynchronous request and to update the content. xajax, on the other hand, allows you to easily control your content from php. the xajaxresponse class allows you to create xml instructions to return to your application from your php functions. the xml is parsed by xajax message pump and the instructions tell xajax how to update the content and state of your application. the xajaxresponse class currently offers a number of useful commands, such as assign, which sets the specified attribute of an element in your page; append, which appends data to the end of the specified attribute of an element in your page; prepend, which prepends data to the beginning of the specified attribute of an element in your page; replace, which searches for and replaces data in the specified attribute of an element in your page; script, which runs the supplied javascript code; and alert, which shows an alert box with the supplied message text.
xajax最独特的长处也许就是 xajaxresponse class了。其它的ajax库需要你亲自写用js写回调的句柄,来处理一个异步请求而且得到的数据,并更新其内容。另一方面,xajax只需你简单的控制好php的内容。然后通过xajaxresponse 类,使在你的php函数中创建xml指令返回给你的程序。xml将被 xajax的信息(pump)解析。其指令告知xajax将如何更新内容和你程序中的位置。现在xajaxresponse 已经提供了大量并有帮助的指令:http://www.flaspx.com/weblog/blog.php?bid=16  (略...付上详细的xajaxresponse 类说明)

a single xml response may contain multiple commands, which will be executed in the order they were added to the response. for example, let's say that a user clicks on a button in your application. the onclick event calls the javascript wrapper for a php function. that wrapper sends an asynchronous request to the server through xmlhttprequest where xajax calls the php function. the php function does a database lookup, some data manipulation, or serialization. you use the xajaxresponse class to generate an xajax xml response containing multiple commands to send back to the xajax message pump to be executed:
一个单独xml响应可以包含多条命令,他们将依据加入响应的顺序来被执行。举个例子吧,让我们假设一个用户在你的程序中按下了一个按钮。这个按下的事件将调用被js封装好的php函数。这个封包通过 xmlhttprequest 发出了一个异步请求给服务器,让xajax调用php函数。这个php函数做了一个查询数据库,一些数据处理或排序的操作。而你要用 xajaxresponse 类来产出一个 xajax 的xml响应,它包含了多条命令。送给xajax 信息pump来执行:


$objresponse = new xajaxresponse();

$objresponse-addassign("myinput1","value",$datafromdatabase);
$objresponse-addassign("myinput1","style.color","red");
$objresponse-addappend("mydiv1","innerhtml",$datafromdatabase2);
$objresponse-addprepend("mydiv2","innerhtml",$datafromdatabase3);
$objresponse-addreplace("mydiv3","innerhtml","xajax","strongxajax/strong");
$objresponse-addscript("var x = prompt("enter your name");");

return $objresponse-getxml();

the xajax message pump would parse the xml message and perform the following:
xajax信息pump将会解析下列xml信息,并执行以下操作:

the value of the element with id myinput1 would be assigned to the data in $datafromdatabase.
将变量$datafromdatabase赋值给id为myinput1的value元素。

the color of the text in the element with id myinput1 would be changed to red.
id为myinput1的字体颜色元素将被换成红色.

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

the data in $datafromdatabase2 would be appended to the innerhtml of the element with id mydiv1.
$datafromdatabase2,此数据将被追加到id为mydiv1的innerthml元素的结束部位

the data in $datafromdatabase3 would be prepended to the innerhtml of the element with id mydiv2.
$datafromdatabase3,此数据将被添加到id为mydiv2的innerthml元素的开始部位

all occurrences of "xajax" in the innerhtml of the element with id mydiv3 would be replaced with "xajax"; making all of the instances of the word xajax appear bold.
id为mydiv3的innerhtml元素中所有的 "xajax" 将被替换成 "xajax",使所有的xajax以粗体显示。

a prompt would be displayed asking for the user's name and the value returned from the prompt would be placed into a javascript variable named x.
会有一个输入框弹出,并询问用户姓名。从输入框取得的变量将转换成js变量并命名为x。
all of this is implemented on the server side in the php function by forming and returning an xajax xml response.
所有这些组成了php函数在服务器端被执行,然后传回一个xml响应。

来源:https://www.tulaoshi.com/n/20160219/1624158.html

延伸阅读
标签: 跳绳
家长睡得晚孩子跟着“难眠” 多数孩子发生睡眠障碍,源于家长的抚养习惯。广东省中医院心理睡眠专科负责人李艳教授表示。 家长睡得晚 孩子跟着难眠 广州市民睡眠障碍发病率高达47%,是国内难眠城市之一!更令人惊讶的是,中国有25%的儿童有睡眠障碍!其中0-5岁儿童睡眠障碍总患病率20.8%。流行病学调查显示,广州增城1099名2-...
十分钟咖喱 1.肉类蔬菜都切成适口大小,咖喱粉和淀粉混合,加入少许水调成糊状 2.平底锅加热,加入少许底油煎香肉类 ...
标签: 分娩
运动胎教是指孕妈妈适时、适当地进行体育锻炼和帮助胎儿活动,以促进胎儿大脑及肌肉的健康发育。研究表明,凡是在宫内受过“体育”运动训练的胎儿,出生后翻身、坐立、爬行、走路及跳跃等动作的发育都明显早于一般的宝宝。俗话说心灵手巧,可见二者是密切联系的。 运动训练可于妊娠3~4个月开始。训练时孕妇应仰卧,全身尽量放松,先用...
标签: 减肥方法 瘦身
大家都知道最健康的减肥就是需要我们不断地运动,作为我们的女性朋友最关注的就是减肥,但是最怕的就是节食减肥和运动减肥。现在天气渐渐冷了。我们的女性根本就是懒得去运动。那么,我们到底该如何去减肥呢?别着急,让图老师小编来教你几招。下面就一起来看看吧。 我们都知道想要真正健康的减肥,是需要我们平时不断的运动。但只要...
标签: 淡奶油
十分钟做蛋挞 步骤 1.将淡奶油、牛奶和炼乳混合并搅拌均匀。 2.加入白砂糖后,开小火边加热边搅拌至白砂糖完全融化。然后马上关火,不要把牛奶烧开哦~ 3.待牛奶晾凉后,倒入打散的蛋黄并搅拌均匀。 4.将低筋面粉晒入蛋黄液中,用橡皮刮刀搅拌均匀至没有面疙瘩。 5.将搅拌好的蛋黄液再次过筛一遍。 6.将过筛后的蛋液倒入...

经验教程

577

收藏

59
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部