下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

教你十分钟学会 xajax

作者:课课家教育     来源: http://www.kokojia.com/点击数:1336发布时间: 2015-11-16 12:24:09

标签: Web开发教程

大神带你学编程,欢迎选课

欢迎来到小编的文章进行学习阅读,想必大家又有很多问题吧,在这里会有你想要收获的答案,请大家慢慢学习吧。excel教学

译者按: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 yourtags, tell xajax to generate the necessary javascript:
在该页的和标签之间插入下列代码,使xajax实例可以自己生成所必需的js

以下为引用的内容:
printjavascript(); ?>
call the function from a javascript event or function in your application:


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

以下为引用的内容:
 

excel基础教程

well高级语言的出现使得计算机程序设计语言不再过度地依赖某种特定的机器或环境。这是因为高级语言在不同的平台上会被编译成不同的机器语言,而不是直接被机器执行。最早出现的编程语言之一FORTRAN的一个主要目标,就是实现平台独立。[1]
虽然大多数的语言可以既可被编译又可被解译,但大多数仅在一种情况下能够良好运行。在一些编程系统中,程序要经过几个阶段的编译,一般而言,后阶段的编译往往更接近机器语言。这种常用的使用技巧最早在1960年代末用于BCPL,编译程序先编译一个叫做“0代码”的转换程序(representation),然后再使用虚拟器转换到可以运行于机器上的真实代码。这种成功的技巧之后又用于Pascal和P-code,以及Smalltalk和二进制码,在很多时候,中间过渡的代码往往是解译,而不是编译的。[1]
如果所使用的翻译的机制是将所要翻译的程序代码作为一个整体翻译,并之后运行内部格式,那么这个翻译过程就被成为编译。因此,一个编译器是一个将人可阅读的程序文本(叫做源代码)作为输入的数据,然后输出可执行文件(object code)。所输出的可执行文件可以是机器语言,由计算机的中央处理器直接运行,或者是某种模拟器的二进制代码。[1]
如果程序代码是在运行时才即时翻译,那么这种翻译机制就被称作直译。经直译的程序运行速度往往比编译的程序慢,但往往更具灵活性,因为它们能够与执行环境互相作用。[1]

 


只需这些步骤。其他的交由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","xajax");
$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响应。excel教程

更多视频课程文章的课程,可到课课家官网查看。我在等你哟。

赞(100)
踩(2)
分享到:
华为认证网络工程师 HCIE直播课视频教程