prototype 学习手记

来源:互联网 发布:java软件开发面试 编辑:程序博客网 时间:2024/06/04 22:46
 对于客户端和服务器端的通讯,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])
   相当于根据指定的时间,重复调用Updater,更新目标对象。
   可以通过参数的设置,当其发现本次访问返回内容跟上一次相同时,对间隔时间乘以指定的系数。
   如果发现返回内容修改后,间隔时间调整为初始设置值。
   问题:
       是否考虑在发现内容修改后,将间隔时间设置为 当前值/系数 (并不小于初始值)?
       这样是否更合理?

原创粉丝点击