jquery.unobtrusive-ajax.js单独的用法

时间:2023-03-08 23:37:38
jquery.unobtrusive-ajax.js单独的用法

(插件本身已经减少了人力,如果开始无脑开发,简直就是无能,@Ajax里面哪里帮助类生成的其实就是jquery.unobtrusive的一些特性)

jquery.unobtrusive是MVC中出现的jquery插件,用与和MVC中Ajax交互,但是我不怎么喜欢在构建页面的时候用@Ajax.XXX去构建,感觉太依赖了,jquery.unobtrusive应该是所有web后端语言都能用的东西。所以就有了自己的单独使用jquery.unobtrusive的旅程,研究jquery.unobtrusive的用法和源码(之前已经有过阅读源码的文章和一定的注释,地址:http://www.cnblogs.com/RainbowInTheSky/p/4466993.html)。

用法data-ajax="true"是开启jquery.unobtrusive。

$(element.getAttribute("data-ajax-update"))

  源码中有这样一段,说明data-ajax-update的值就是选择器得到的,可以直接#id,.class。(data-ajax-update//更新的对象)

经常与data-ajax-mode配合使用

data-ajax-mode//更新的形式 BEFORE插入到对象之前 AFTER插入到对象之后 为空就是覆盖

  函数调用

data-ajax-begin
data-ajax-complete
data-ajax-success
data-ajax-failure

上面的四个属性对应$.ajax中的beforeSend,complete,success,error,他们的参数可以是程序片段,也可以是一个function,但是在写参数的时候不能是functiong(){}这样的匿名函数。基本涵盖了ajax,满足了需求。

不过在使用

data-ajax-loading//显示loading的对象

data-ajax-loading-duration//持续时间 默认是0

这两个属性的时候需要扩展一下,因为jquery.unobtrusive没有对齐做UI的实现,需要自己扩展。data-ajax-loading就是需要显示的对象,data-ajax-loading-duration是持续时间,他们使用的是jquery.show(),和jquery.hide()这两个函数。为此我写了一个遮罩的扩展

 //创建简单遮罩层,显示load时的信息,配合Unobtrusive
; (function ($) {
//设置背景层高
function height() {
var scrollHeight, offsetHeight;
// handle IE 6
if ($.browser.msie && $.browser.version < 7) {
scrollHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
offsetHeight = Math.max(document.documentElement.offsetHeight, document.body.offsetHeight);
if (scrollHeight < offsetHeight) {
return $(window).height();
} else {
return scrollHeight;
}
// handle "good" browsers
}
else if ($.browser.msie && $.browser.version == 8) {
return $(document).height() - 4;
}
else {
return $(document).height();
}
};
//设置背景层宽
function width() {
var scrollWidth, offsetWidth;
// handle IE
if ($.browser.msie) {
scrollWidth = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth);
offsetWidth = Math.max(document.documentElement.offsetWidth, document.body.offsetWidth);
if (scrollWidth < offsetWidth) {
return $(window).width();
} else {
return scrollWidth;
}
// handle "good" browsers
}
else {
return $(document).width();
}
};
/*==========全部遮罩=========*/
function createLayer(load) {
//背景遮罩层
var layer = $("<div id=" + load.attr("data-loadlayer-id") + "></div>");
layer.css({
zIndex: 9998,
position: "absolute",
height: height() + "px",
width: width() + "px",
background: "black",
top: 0,
left: 0,
filter: "alpha(opacity=30)",
opacity: 0.3
}); //图片及文字层
var content = $("<div id=" + load.attr("data-loadlayer-id") + "-content" + "></div>");
content.css({
textAlign: "left",
position: "absolute",
zIndex: 9999,
//height: opt.height + "px",
//width: opt.width + "px",
top: "50%",
left: "50%",
verticalAlign: "middle",
//background: opt.backgroudColor,"#ECECEC"
background: "#ECECEC",
borderRadius: "3px",
padding:"2px 5px 2px 5px",
fontSize: "13px"
}); //content.append("<img style='vertical-align:middle;margin:" + (opt.height / 4) + "px; 0 0 5px;margin-right:5px;' src='" + opt.backgroundImage + "' /><span style='text-align:center; vertical-align:middle;'>" + opt.text + "</span>");
content.append("<span style='text-align:center; vertical-align:middle;color:#000000'>" + load.attr("data-loadlayer-msg") + "</span>");
load.append(layer.append(content));
var top = content.css("top").replace('px', '');
var left = content.css("left").replace('px', '');
content.css("top", (parseFloat(top) - parseFloat(content.css("height")) / 2)).css("left", (parseFloat(left) - parseFloat(content.css("width")) / 2)); layer.hide();
return this;
} $(document).ready(function () {
createLayer($("div[data-loadlayer=true]"))
});
})(jQuery)

使用方法

 <div data-loadlayer="true" data-loadlayer-id="load" data-loadlayer-msg="loading..."></div>

然后需要在调用的地方data-ajax-loading="#load"即可