prototype 学习手记(2)

对于客户端和服务器端的通讯,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, 所有

你可能感兴趣的:(开发手记,prototype,xmlhttprequest,parameters,function,object,exception)