(function ($){
$.fn.extend({
exajax:function(url,opts,convert){
var ajaxParam = {
url:url,
async : false, // 同步
type:'POST', // 請求類型
cache:false, // 不緩存
dataType:'json',
success:'',
error : ''
};
opts =opts||{};
for(var k in opts){
if(ajaxParam.hasOwnProperty(k)||'data'==k){
if(k=='data'){
try{
var params = opts['data'];
if(!convert){
params = JSON.stringify(params);
ajaxParam.contentType= 'application/json';
}
ajaxParam.data= params;
}catch(e){
}
}else
if(k=='success'){
var success = opts[k];
ajaxParam[k] =function(data){
if(success){
if(typeof success == 'function'){
success.apply(success,[data]);
}else{
if(success.message){
BootstrapDialog.show({
title: success.title||'系統消息',
message: success.message||'操做成功',
buttons: [{
label: '肯定',
action: function(dialogItself){
dialogItself.close();
}
}]
});
}
}
}
}
}else
if(k=='error'){
var error = opts[k];
ajaxParam[k] =function(e) {
if(error){
if(typeof error == 'function'){
error.apply(error,[e]);
}else{
if(error.message){
BootstrapDialog.show({
title: error.title||'系統錯誤',
message: error.message||'系統未知錯誤 - '+e.description,
buttons: [{
label: '肯定',
action: function(dialogItself){
dialogItself.close();
}
}]
});
}
}
}
}
}else{
ajaxParam[k] = opts[k];
}
}
}
$.ajax(ajaxParam);
},
getJsonFormValues:function(){
el = $(this);
var elementsObj = el.get(0).elements;
var ret = {};
if(elementsObj){
$.each(elementsObj,function(index, obj){
val = null;
if (obj.tagName == "INPUT" || obj.tagName == "SELECT") {
val = obj.value;
if(obj.name == "id" && obj.value == ""){
val = null;
}
if(obj.type == 'checkbox' || obj.type == 'radio'){
if($(obj).is(':checked')){
var result;
if(typeof obj.value !=undefined){
result = obj.value;
}
if(obj.type == 'radio'){
ret[obj.name]=result;
}else{
if(!ret[obj.name]){
ret[obj.name] = [];
}
ret[obj.name].push(result);
}
}
}
if(obj.type != 'checkbox' && obj.type != 'radio'){
ret[obj.name] = val;
}
}
});
}
return ret;
},
getFormValues:function(){
el = $(this);
var result = $(this).serialize();
//var elementsObj = el.get(0).elements;
//var ret = {};
//if(elementsObj){
//$.each(elementsObj,function(index, obj){
//if(obj.type == 'hidden'){
//result += "&" +obj.name+"="+obj.value;
//}
//});
//}
return result;
},
getFormValueNoHidden:function(){
el = $(this);
var result = $(this).serialize();
var elementsObj = el.get(0).elements;
var ret = {};
if(elementsObj){
$.each(elementsObj,function(index, obj){
});
}
return result;
},
setFormValues:function(data){
el = $(this);
var elementsObj = el.get(0).elements;
if (elementsObj) {
$.each(elementsObj,function(index, obj){
if (obj.tagName == "INPUT" || obj.tagName == "SELECT") {
val = null;
if(data[obj.name] != undefined){
var func = data[obj.name];
if(typeof(func)=='function'){
val = func.apply(func,[val,obj,elementsObj]);
}else{
val = func;
}
}
//if(format&&format[obj.name]!= undefined){
//func = format[obj.name];
//if(typeof(func)=='function'){
//val = func.apply(func,[val,obj,data]);
//}else{
//val = func;
//}
//}
if(obj.type == 'checkbox' || obj.type == 'radio'){
if(obj.value !=undefined && val instanceof Array){
for(var v in val){
if(val[v]==obj.value){
$(obj).prop("checked",true);
}
}
}else{
if(null!=val &&val!=undefined&&(val == true || val == 1||val==obj.value)){
$(obj).prop("checked",true);
}else{
$(obj).prop("checked",false);
}
}
}else if(obj.type=='file'){
// do nothing;
}else{
obj.value = val;
}
}
});
}
}
});
})(jQuery);ajax