kako si kej ?

jQuery 1.0.2

In 程式設計 on 2006 年 10 月 12 日 at 22:37

手上的某個專案因為歷史的關係:?:,所以先後分別用了 prototypejQuery 兩個 JavaScript library,今天想了一下,還是決定統一用一個 library 就好了;由於 jQuery 看起來較輕薄短小(其實 pack 起來後兩個差不多大),加上 Chainable、支援 XPath 等一些像我這種懶人喜愛的特性的緣故,決定全部使用 jQuery。

程式都改完、測試好之後才想到該去 jQuery 官網看看,發現 10/9 剛發佈了新的 1.0.2 版,於是就下載回來更新(之前是用 1.0.1 版),沒想到一更新之後所有 $.ajax callback 的部份全都失效了,查了一下 API 發現原來在 $.ajax 的地方是這樣寫的:

(Function) error – A function to be called if the request fails. The function gets passed two arguments: The XMLHttpRequest object and a string describing the type of error that occurred.

(Function) success – A function to be called if the request succeeds. The function gets passed one argument: The data returned from the server, formatted according to the ‘dataType’ parameter.

(Function) complete – A function to be called when the request finishes. The function gets passed two arguments: The XMLHttpRequest object and a string describing the type the success of the request.

而我的程式在 success 時的 callback function 卻也是以回傳一個 XMLHttpRequest 物件的方式來寫的,因此當事實上回傳的已經是一個字串時,也就不會有 responseText 囉(因為回傳值本身就是responseText),然後 callback function 就理所當然的死掉了。

當我再把 1.0.1 版蓋回去測試時,卻發現程式跑起來一切正常,一丁點錯誤訊息也沒有;原來 1.0.1 版 success 的部份的確是回傳一個 XMLHttpRequest 物件,所以或許是在 1.0.2 版有作更改了吧,不過我怎樣也找不到哪邊有 change log 提到這點,也找不到舊版的 API 文件來作對照,但是經過一連串的交叉測試之後應該可以確定是這樣子沒錯。所以這下子有兩個選擇:一個是把 success 改成 complete,這樣就不用去更動 callback function,另一個是將 callback function 內的 XMLHttpRequestObject.responseText 改成直接用 responseText,亦即將

function successCallBack(XMLHttpRequestObject){
eval("var jsonVars = " + XMLHttpRequestObject.responseText);
………
}

改成

function successCallBack(responseText){
eval("var jsonVars = " + responseText);
………
}

而我選的是後者。

喔,其實還有另一個選擇,就是用舊版 library,但,何必咧?

真不知道為什麼要將 success 的部份設計的跟另外兩個狀態不一樣啊……

2006-10-14 update :
jQuery 的官方部落格今天提到了這點:其實這項 API 的變動,原本是預計等到 1.1 版的 major release 時才會變更的,結果「不小心」在 1.0.2 時就先用了,然後又順便忘了提到這點……

post this page to twitter via twitthat

No TweetBacks yet.
  1. 保鏢 說道:

    謝謝您前來敝人的部落格!:)

  2. pawn shops tampa 說道:

    pawn shops tampa

    jQuery 1.0.2 « Kej.tw Revisited

Comments are closed.

    follow me on Twitter