/*! ** Unobtrusive Ajax support library for jQuery ** Copyright (C) Microsoft Corporation. All rights reserved. */ /*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: false */ /*global window: false, jQuery: false */ /* data-ajax=true //開啓綁定 data-ajax-mode//更新的形式 BEFORE插入到對象以前 AFTER插入到對象以後 爲空就是覆蓋 data-ajax-update//更新的對象 data-ajax-confirm//設置一個肯定取消彈出框的文字,沒有則不設置 data-ajax-loading//顯示loading的對象 data-ajax-loading-duration//持續時間 默認是0 data-ajax-method//提交方式 data-ajax-url//提交url data-ajax-begin//ajax前觸發的函數或者一段程序 data-ajax-complete//完成後,此時尚未加載返回的數據,請求成功或失敗時均調用 data-ajax-success//成功,加載完成的數據 data-ajax-failure//失敗 */ (function ($) { var data_click = "unobtrusiveAjaxClick", data_validation = "unobtrusiveValidation"; //第二核心,判斷是否函數,不是則構造一個匿名函數 function getFunction(code, argNames) { var fn = window, parts = (code || "").split("."); while (fn && parts.length) { fn = fn[parts.shift()]; }//查找函數名有時候是命名空間好比xxx.xxx if (typeof (fn) === "function") { return fn; } argNames.push(code); //若是不是函數對象則本身構造一個並返回,吊! return Function.constructor.apply(null, argNames); } function isMethodProxySafe(method) { return method === "GET" || method === "POST"; } //能夠添加各類提交方式,應該是爲Web Api作的補充 function asyncOnBeforeSend(xhr, method) { if (!isMethodProxySafe(method)) { xhr.setRequestHeader("X-HTTP-Method-Override", method); } //注:X-HTTP-Method-Override是一個非標準的HTTP報頭。 //這是爲不能發送某些HTTP請求類型(如PUT或DELETE)的客戶端而設計的 } //完成後的 function asyncOnSuccess(element, data, contentType) { var mode; if (contentType.indexOf("application/x-javascript") !== -1) { // jQuery already executes JavaScript for us return; } mode = (element.getAttribute("data-ajax-mode") || "").toUpperCase(); $(element.getAttribute("data-ajax-update")).each(function (i, update) { var top; switch (mode) { case "BEFORE": top = update.firstChild; $("<div />").html(data).contents().each(function () { update.insertBefore(this, top); }); break; case "AFTER": $("<div />").html(data).contents().each(function () { update.appendChild(this); }); break; default: $(update).html(data); break; } }); } //主要函數 //綁定的對象和參數 function asyncRequest(element, options) { var confirm, loading, method, duration; confirm = element.getAttribute("data-ajax-confirm"); if (confirm && !window.confirm(confirm)) { return; } loading = $(element.getAttribute("data-ajax-loading"));// duration = element.getAttribute("data-ajax-loading-duration") || 0;//默認是0 $.extend(options, { type: element.getAttribute("data-ajax-method") || undefined, url: element.getAttribute("data-ajax-url") || undefined, beforeSend: function (xhr) {//ajax前觸發,此處的xhr將在下面用apply傳遞出去 var result; asyncOnBeforeSend(xhr, method);//判斷是否添加特種的提交方式 result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(this, arguments);//argument:替換函數對象的其中一個屬性對象,存儲參數。這裏是將原先的參數傳遞出去,吊! if (result !== false) { loading.show(duration); } return result; }, complete: function () { loading.hide(duration); getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(this, arguments); }, success: function (data, status, xhr) { asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html"); getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(this, arguments); }, error: getFunction(element.getAttribute("data-ajax-failure"), ["xhr", "status", "error"]) }); options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" }); method = options.type.toUpperCase();//大寫 if (!isMethodProxySafe(method)) { options.type = "POST"; options.data.push({ name: "X-HTTP-Method-Override", value: method }); } //最後都是調用jquery的ajax $.ajax(options); } function validate(form) { //能夠取消驗證 var validationInfo = $(form).data(data_validation); return !validationInfo || !validationInfo.validate || validationInfo.validate(); } $(document).on("click", "a[data-ajax=true]", function (evt) { evt.preventDefault(); asyncRequest(this, { url: this.href, type: "GET", data: [] }); }); $(document).on("click", "form[data-ajax=true] input[type=image]", function (evt) {//這個不經常使用 var name = evt.target.name, $target = $(evt.target), form = $target.parents("form")[0], offset = $target.offset(); $(form).data(data_click, [ { name: name + ".x", value: Math.round(evt.pageX - offset.left) }, { name: name + ".y", value: Math.round(evt.pageY - offset.top) } ]); setTimeout(function () { $(form).removeData(data_click); }, 0); }); $(document).on("click", "form[data-ajax=true] :submit", function (evt) { var name = evt.target.name, form = $(evt.target).parents("form")[0]; $(form).data(data_click, name ? [{ name: name, value: evt.target.value }] : []); setTimeout(function () { $(form).removeData(data_click); }, 0); }); $(document).on("submit", "form[data-ajax=true]", function (evt) { var clickInfo = $(this).data(data_click) || []; evt.preventDefault(); if (!validate(this)) { return; } asyncRequest(this, { url: this.action, type: this.method || "GET", data: clickInfo.concat($(this).serializeArray())//寫得好,序列化表單並拼接,之後的ajax均可以這樣,方便啊 }); }); }(jQuery));
基本上想註釋的都註釋了一遍,我反正被這180行的代碼折服了。很不錯!javascript