十分钟学会 xajax

ajax

译者按: 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 >

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","<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响应。

时间: 2024-08-03 13:50:08

十分钟学会 xajax的相关文章

十分钟学会Java RMI

RMI是Java平台实现远程调用的规范,下面是一个小例子,本机测试通过 一共有三个java类,远程接口,服务端程序,客户端程序 远程接口: import java.rmi.*; public interface HelloIn extends java.rmi.Remote{ String sayHello() throws RemoteException; } 服务端程序: /** * author by http://www.bt285.cn http://www.5a520.cn */ i

使用ztree.js,受益一生,十分钟学会使用tree树形结构插件

看到ztree.js,这几个字眼,毋庸置疑,那肯定就是tree树形结构了,曾经的swing年代有jtree,后来jquery年代有jstree和treeview,虽然我没写过,但是我见过,一些小功能做起来比较繁琐,几个功能做了一个月,当然了有能力的关系,也有插件易用度的关系 最近要实现的功能用tree树形结构来做是最好的,就是资源的分配,来简单讲讲ztree吧,这是目前国内很火的一个插件,百度一下就能了解,使用起来十分简单 小编我用过3年的jqgrid,在此还是要再次感谢当年的王组让我去跟jqg

神速鼠标连点!制作只要十分钟!

玩游戏最常用的工具有哪些?想必当属鼠标连点,键盘连按系列了!这些个工具可算是跨时代的"低智商"发明了!今天就让按键精灵帮你解密各种奥秘吧!让你十分钟学会鼠标连点脚本! 首先打开按键精灵,选择"新建"按钮,现在看到的这个界面,就是脚本编辑器了.如下图: 图1 接下来我们先要进行一项准备工作,就是要先在脚本属性区这里,把脚本描述和热键改掉. 我的描述改成"鼠标连点",启动热键改成Ctrl+1,终止热键改成Ctrl+2.更改描述是为了我们往后更好的调用

UWP开发入门(十九)——10分钟学会在VS2015中使用Git

原文:UWP开发入门(十九)--10分钟学会在VS2015中使用Git 写程序必然需要版本控制,哪怕是个人项目也是必须的.我们在开发UWP APP的时候,VS2015默认提供了对微软TFS和Git的支持.考虑到现在Git很火,作为微软系的程序员也不得不学一点防身,以免被开源世界的家伙们嘲笑.蜀黍我Git也是菜鸟一只(还请老司机多多指点),只会用VS2015和SourceTree这样的GUI工具点一点按钮,但是我相信用惯了SVN和TFS的童鞋们,需要一点勇气去学习一些新东西,特别是Git已经形成潮

10分钟学会理解和解决MySQL乱码问题

原文:10分钟学会理解和解决MySQL乱码问题 在阅读本文之前,强烈建议对字符集编码概念还比较模糊的同学 阅读下博主之前对相关概念的一篇科普:十分钟搞清字符集和字符编码 本博客已经迁移至: http://cenalulu.github.io/ 为了更好的体验,请通过此链接阅读: http://cenalulu.github.io/mysql/mysql-mojibake/ MySQL出现乱码的原因 要了解为什么会出现乱码,我们就先要了解从客户端发起请求,到MySQL存储数据,再到下次从表取回客户

五分钟学会部署OpenStack

问题描述 大家好!我是小鸥,刚刚上手学习OpenStack,今天和大家一起分享一下最近的学习心得,欢迎和我一起交流.第一讲『小鸥』教你5分钟学会部署OpenStack今天先来一个5分钟教你学会部署OpenStack,通过本次学习仅需大概1个小时就可以在VirtualBox环境中部署好一个OpenStack集群.准备环境本实验建议主机最少配置8G内存(最好16G),可以在MacOS10.7.x/10.8.x.CentOS6.4或Ubuntu12.04中运行本教程.为了在VitualBox中成功安装

《醉打山门》“鲁智深”单腿立地近二十分钟

曹志威在表演<醉打山门> 单腿立地近二十分钟 说起昆曲,很多人首先联想到的是才子佳人.花前月下的文戏.事实上,昆曲中的武戏也很是精彩.5日,全国昆曲十佳新秀榜首--曹志威在江苏省演艺集团昆剧院兰苑小剧场举办了个人专场.舞台上,他的绝活引来叫好声一片.在<醉打山门>中,他饰演的鲁智深以右腿单腿立地近二十分钟,其间不断表演各种动作,甚至单腿下蹲,但全身依然稳如泰山,令人叹为观止. 撰文 快报记者 沈梅 能单腿立地二十多分钟 事实上,<醉打山门>正是曹志威的成名之作.该剧讲述

Emacs之魂(二):一分钟学会人界用法

Emacs之魂(一):开篇Emacs之魂(二):一分钟学会人界用法Emacs之魂(三):列表,引用和求值策略Emacs之魂(四):标识符,符号和变量Emacs之魂(五):变量的"指针"语义Emacs之魂(六):宏与元编程Emacs之魂(七):变量捕获与卫生宏Emacs之魂(八):反引用与嵌套反引用Emacs之魂(九):读取器宏 上文提到了编辑器之战, 据江湖传说,Emacs被称为"神的编辑器", Emacs有着无与伦比的可扩展性和可定制性,简直变成了一个"

ios-设置按钮在十分钟之内禁用

问题描述 设置按钮在十分钟之内禁用 ios应用中的button: _Button = [UIButton buttonWithType:UIButtonTypeCustom]; UIImage *shareIMG = [UIImage imageNamed:@"button.png"]; [_Button setBackgroundImage:shareIMG forState:UIControlStateNormal]; [_Button setBackgroundImage:[UI