对于客户端和服务器端的通讯,prototype提供了如下技术手段:
Ajax.PeriodicalUpdater
Ajax.Request
Ajax.Responders
Ajax.Response
Ajax.Updater
在调用过程中,prototype支持一些常用的option选型,和回调事件
Common options
Common callbacks
除此之外,对于特定的对象,还有一些特定的option和事件
1. Ajax.Request
Request应当是最基本的对象,调用语法:
new Ajax.Request(url[, options])
用于创建一个AJAX request对象,发送http请求,获取http response.
在options中,可以定义:
a) request header属性 和 参数,例如:
{ method: ''get'',
encoding: ''UTF-8'',
parameters: { username: $F(''username'') }
}
b) 回调事件和方法,例如:
{
onCreate: function() {
Ajax.activeRequestCount++;
},
onComplete: function() {
Ajax.activeRequestCount--;
}
}
Life cycle of XMLHtpRequest:
1) Created
2) Initialized
3) Request sent
4) Response being received (can occur many times, as packets come in)
5) Response received, request complete
Order of callbacks:
1) onCreate
2) onUninitialized (maps on Created)
3) onLoading (maps on Initialized)
4) onLoaded (maps on Request sent)
5) onInteractive (maps on Response being received)
6) onXYZ (numerical response status code), onSucess or onFailure
7) onComplete
2. Ajax.Response
从1.6版本开始提供
对xmlHttpRequest对象进行了包装,处理跨浏览器问题,并添加了对JSON的支持
作为第一个参数,传递给各个callback方法
属性:
status (Number) http status code
statusText (String)
readyState (Number)
responseText (String)
responseXML (document Object or null) applicable when content-type=application/xml
responseJSON (Object, Array or null) applicable when content-type=application/json
headerJSON (Object, Array or null) applicable for X_JSON header
request (Object) the request object (instance of Ajax.Request or Ajax.Updater)
transport (Object) the native xmlHttpRequest object
方法:
getHeader(name) String or null 找不到时返回null,不会抛出Exception
getAllHeaders() String or null 用换行分割
getResponseHeader(name) String 可能抛Exception
getAllResponseHeaders() String 多个item,用换行分隔,可能抛Exception
3. Ajax.Responders
语法:
Ajax.Responders.register(responder)
Ajax.Responders.unregister(responder)
用于对页面中所有的request进行某些公共的操作,例如,记录当前活动的AJAX request数量
Prototype自动注册了如下操作:
Ajax.Responders.register({
onCreate: function() {
Ajax.activeRequestCount++;
},
onComplete: function() {
Ajax.activeRequestCount--;
}
});
如果要取消注册,也需要先定义responder对象。因此,一般在注册时保留responder对象的reference
问题:
注册/取消注册,key是什么?responder对象?还是???
4. Ajax.Updater
语法:
new Ajax.Updater(container, url[, options])
用途:
指定一个对象,修改其内容
样例:
new Ajax.Updater(''items'', ''/items'', {
parameters: { text: $F(''text'') }
});
说明:
onComplete回调方法会在对象内容被修改后再被触发。
附加参数:
1. 缺省情况下,对象内容会被新的内容替代。但可以设置为插入,以及插入的位置
例如:
new Ajax.Updater(''items'', ''/items'', {
parameters: { text: $F(''text'') },
insertion: Insertion.Bottom
});
v1.6.0之后,不再使用Insertion.Bottom这类方式,可以直接使用''top'',''bottom'',''before'', ''after''
2.evalScripts
当其为true, 所有<script>内容将被evaluated.
<script>内容相对于被执行eval()方法,其中的局部变量只再eval()期间有效,不影响页面其他部分。
但如果在其中定义了JavaScript方法,则必须创建function对象,例如:
coolFunc = function() { ... }
而不能使用如下定义:
function coolFunc() { ... }
其他:
Updater还可以根据request是否成功,修改不同的目标对象,例如:
new Ajax.Updater({ success: ''items'', failure: ''notice'' }, ''/items'', {
parameters: { text: $F(''text'') },
insertion: Insertion.Bottom
});
问题:
修改不同目标对象时,处理策略是否只能相同?
如果创建两个Updater对象,一个只处理success,一个只处理failure,是否可以解决这个问题?
5. Ajax.PeriodicalUpdater
语法:
new Ajax.PeriodicalUpdater(container, url[, options])
文章出处:DIY部落(http://www.diybl.com/course/1_web/webjs/2008515/116422.html)
分享到:
相关推荐
Prototype学习笔记(一)(二)的源代码
prototype学习 prototype学习 prototype学习
prototype1.6.0prototype1.6.0prototype1.6.0prototype1.6.0
利用DREAMWEVER开发网站的插件Prototype。
Prototype Ajax学习范例,快速掌握AJAX学习
Prototype学习资料
博文链接:https://jianfeng008cn.iteye.com/blog/41762
3 2. 通用性方法 3 2.1. 使用 $()方法 3 2.2. 使用$F()方法 4 2.3. 使用Try.these()方法 5 3. Ajax 对象 5 3.1. 使用 Ajax.Request类 5 3.2. 使用 Ajax.Updater 类 7 4. prototype.js参考 ...
类之Prototype.js学习
博文链接:https://kdisk-sina-com.iteye.com/blog/239793
prototype.js不仅是一个有很大实用价值的js库,而且有很高的学习价值。 --- 在prototype.js中,prototype对象是实现面向对象的一个重要机制。 每个函数(Function)就是一个对象,函数对象都有一...
对prototype的用法说明
网上找的一些prototype.js学习资料
prototype帮助中文文档,prototype的源代码
prototype1.4 学习资料
Prototype 学习 Prototype对象
prototype.js ajax,javascript框架学习
prototype的JS文件prototype的JS文件prototype的JS文件prototype的JS文件
学习Prototype一段时间整理的一些相关资料,供参考学习!