十分钟学会 xajax

alphatwj 2006-05-21 06:10:25
learn xajax in 10 minutes(原文摘自xajaxproject 官方wiki)
http://wiki.xajaxproject.org/tutorials:learn_xajax_in_10_minutes

翻译:alpha's blog http://www.flaspx.com/weblog/blog.php?bid=15


译者按: 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都要处理所有请求

$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 已经提供了大量并有帮助的指令:(略...付上详细的xajaxresponse 类说明)http://www.flaspx.com/weblog/blog.php?bid=16

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","<strong>xajax</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的字体颜色元素将被换成红色.

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响应。
...全文
389 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Scarroot 2006-05-23
  • 打赏
  • 举报
回复
mark
TSD 2006-05-22
  • 打赏
  • 举报
回复
MARK
sdts 2006-05-22
  • 打赏
  • 举报
回复
来个asp的,这个看不懂
showrock 2006-05-22
  • 打赏
  • 举报
回复
来个C#的,这个看不懂

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧