重构代码:重构$.bc方法,精简代码

This commit is contained in:
Argo-MacBookPro 2018-09-09 13:19:05 +08:00
parent 408dca0eb4
commit 1cc8b4d7fc
16 changed files with 64 additions and 82 deletions

View File

@ -24,7 +24,7 @@
nestMenu: function (callback) {
var $this = $(this);
$.bc({
id: 0, url: Menu.url, data: { type: "user" },
id: 0, url: Menu.url, data: { type: "user" }, method: "post",
callback: function (result) {
var html = "";
if ($.isArray(result)) html = cascadeMenu(result);
@ -47,7 +47,6 @@
var that = this;
$.bc({
url: Notifications.url,
method: 'GET',
callback: function (result) {
$('#logoutNoti').text(result.NewUsersCount === 0 ? "" : result.NewUsersCount);
that.clearWidgetItems();

View File

@ -24,7 +24,7 @@
// autocomplete
$.bc({
url: "api/Category", method: 'get',
url: "api/Category",
callback: function (result) {
var data = result.map(function (ele, index) { return ele.Category; });
$('#txt_dict_cate').typeahead({

View File

@ -36,7 +36,7 @@
$('#btn_view').on('click', function (row) {
$.bc({
url: Exceptions.url,
url: Exceptions.url, method: "post",
callback: function (result) {
var html = result.map(function (ele) {
return $.format('<div class="form-group col-lg-3 col-md-3 col-sm-4 col-6"><a class="logfile" data-toggle="tooltip" title="{0}" href="#"><i class="fa fa-file-text-o"></i><span>{0}</span></a></div>', ele);
@ -54,7 +54,7 @@
$errorDetail.show();
$dataFormDetail.html('<div class="text-center"><i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i></div>');
$.bc({
url: Exceptions.url, method: "PUT", data: { FileName: fileName },
url: Exceptions.url, method: "put", data: { FileName: fileName },
callback: function (result) {
$dataFormDetail.html(result);
}

View File

@ -17,7 +17,7 @@
events: {
'#btn_assignRole': function (row) {
$.bc({
id: row.Id, url: Role.url, data: { type: "group" },
id: row.Id, url: Role.url, data: { type: "group" }, method: "post",
callback: function (result) {
var htmlTemplate = this.htmlTemplate;
var html = $.map(result, function (element, index) {
@ -33,7 +33,7 @@
},
'#btn_assignUser': function (row) {
$.bc({
id: row.Id, url: User.url, data: { type: "group" },
id: row.Id, url: User.url, data: { type: "group" }, method: "post",
callback: function (result) {
var htmlTemplate = this.htmlTemplate;
var html = $.map(result, function (element, index) {
@ -52,14 +52,14 @@
var roleIds = $dialogRole.find('input:checked').map(function (index, element) {
return $(element).val();
}).toArray().join(',');
$.bc({ id: groupId, url: Role.url, method: "PUT", data: { type: "group", roleIds: roleIds }, title: Role.title, modal: '#dialogRole', info: true });
$.bc({ id: groupId, url: Role.url, method: "put", data: { type: "group", roleIds: roleIds }, title: Role.title, modal: '#dialogRole' });
},
'#btnSubmitUser': function (row) {
var groupId = row.Id;
var userIds = $dialogUser.find(':checked').map(function (index, element) {
return $(element).val();
}).toArray().join(',');
$.bc({ id: groupId, url: User.url, method: "PUT", data: { type: "group", userIds: userIds }, title: User.title, modal: '#dialogUser', info: true });
$.bc({ id: groupId, url: User.url, method: "put", data: { type: "group", userIds: userIds }, title: User.title, modal: '#dialogUser' });
}
}
},

View File

@ -1,16 +1,16 @@
(function ($) {
logPlugin = function (options) {
this.options = $.extend({}, logPlugin.settings, options);
}
};
logPlugin.settings = {
url: '../api/Logs',
url: 'api/Logs',
click: {
query: 'btn_query',
del: 'btn_delete',
save: 'btnSubmit'
query: '#btn_query',
del: '#btn_delete',
save: '#btnSubmit'
}
}
};
logPlugin.prototype = {
constructor: logPlugin,
@ -19,9 +19,9 @@
// handler click event
for (name in this.options.click) {
var cId = this.options.click[name];
var source = $("#" + cId);
source.data('click', name);
source.click(function () {
var $source = $(cId);
$source.data('click', name);
$source.click(function () {
var method = $(this).data('click');
logPlugin.prototype[method].call(that, this);
});
@ -36,22 +36,22 @@
del: function () {
log(this.options.url, { crud: '删除' });
}
}
};
logPlugin.init = function (options) {
var log = new logPlugin(options);
log.init();
}
};
var log = function (url, data) {
$.extend(data, { requestUrl: window.location.pathname });
$.post({
url: url,
url: $.formatUrl(url),
data: JSON.stringify(data),
contentType: 'application/json',
dataType: 'json'
});
}
};
})(jQuery);
$(function () {

View File

@ -17,6 +17,7 @@
url: 'api/New',
data: { UserName: $('#userName').val(), Password: $('#password').val(), DisplayName: $('#displayName').val(), Description: $('#description').val() },
modal: '#dialogNew',
method: "post",
callback: function (result) {
var title = result ? "提交成功<br/>等待管理员审批" : "提交失败";
swal({ html: true, showConfirmButton: false, showCancelButton: false, timer: 1500, title: title, type: result ? "success" : "error" });

View File

@ -113,22 +113,19 @@
$.extend({
bc: function (options, callback) {
options = $.extend({
remote: true,
id: "",
url: "",
data: {},
contentType: 'application/json',
dataType: 'json',
method: "post",
htmlTemplate: '<div class="form-group col-md-3 col-sm-4 col-6"><div class="form-check"><label class="form-check-label" title="{3}" data-toggle="tooltip"><input type="checkbox" class="form-check-input" value="{0}" {2}/><span>{1}</span></label></div></div>',
title: "",
modal: null,
modal: false,
loading: false,
loadingTimeout: 10000,
callback: null,
$element: null,
async: true,
info: false
callback: false,
$element: false,
contentType: 'application/json',
dataType: 'json',
method: 'get'
}, options);
if (!options.url || options.url === "") {
@ -144,17 +141,14 @@
}, options.loadingTimeout);
}
if (options.remote && options.url) {
if (options.url) {
var data = options.method === 'get' ? options.data : JSON.stringify(options.data);
$.ajax({
url: $.formatUrl(options.url) + options.id,
data: options.contentType === 'application/json' &&
(options.method.toLowerCase() === 'post' || options.method.toLowerCase() === 'put' || options.method.toLowerCase() === 'delete')
? JSON.stringify(options.data) : options.data,
type: options.method,
data: data,
method: options.method,
contentType: options.contentType,
dataType: options.dataType,
async: options.async,
xhrFields: options.xhrFields,
success: function (result) {
success(result);
},
@ -165,14 +159,14 @@
}
function success(result) {
if ($.isFunction(options.callback)) {
options.callback.call(options.$element === null ? options : options.$element, result);
options.callback.call(options.$element ? options : options.$element, result);
}
if (options.modal !== null && (result || options.loading)) {
if (options.modal && (result || options.loading)) {
$(options.modal).modal('hide');
}
if (options.info) toastr[result ? 'success' : 'error'](options.title + (result ? "成功" : "失败"));
if (options.title) toastr[result ? 'success' : 'error'](options.title + (result ? "成功" : "失败"));
if ($.isFunction(callback)) {
callback.call(options.$element === null ? this : options.$element);
callback.call(options.$element ? this : options.$element);
}
}
},
@ -276,7 +270,6 @@
},
smartTable: function (options) {
var settings = $.extend({
method: 'get', //请求方式(*
toolbar: '#toolbar', //工具按钮用哪个容器
striped: true, //是否显示行间隔色
cache: false, //是否使用缓存默认为true所以一般情况下需要设置一下这个属性*
@ -325,6 +318,7 @@
$.bc({
url: uri,
id: this.sendMessage,
method: "post",
callback: function (result) {
if (!result) {
that.errorHandler.call(that.target);
@ -440,8 +434,7 @@
// Settings
Settings = {
url: 'api/Settings/',
title: '网站设置'
url: 'api/Settings/'
};
// Messages

View File

@ -143,7 +143,7 @@
setTimeout(function () {
var iDs = arrselections.map(function (element, index) { return element.Id; });
$.bc({
url: options.url, data: iDs, method: 'DELETE', title: '删除数据',
url: options.url, data: iDs, method: 'delete', title: '删除数据',
callback: function (result) {
if (result) $(options.bootstrapTable).bootstrapTable('refresh');
handlerCallback.call(that, null, element, { oper: 'del', success: result });
@ -158,7 +158,7 @@
var that = this;
var options = $.extend(true, {}, this.options, { data: this.dataEntity.get() });
$.bc({
url: options.url, data: options.data, title: "保存数据", modal: options.modal, info: true,
url: options.url, data: options.data, title: "保存数据", modal: options.modal, method: "post",
callback: function (result) {
if (result) {
var finalData = null;

View File

@ -42,7 +42,7 @@
events: {
'#btn_assignRole': function (row) {
$.bc({
id: row.Id, url: Role.url, data: { type: "menu" },
id: row.Id, url: Role.url, data: { type: "menu" }, method: "post",
callback: function (result) {
var htmlTemplate = this.htmlTemplate;
var html = $.map(result, function (element, index) {
@ -61,7 +61,7 @@
var roleIds = $dialogRole.find('input:checked').map(function (index, element) {
return $(element).val();
}).toArray().join(',');
$.bc({ id: menuId, url: Role.url, method: "PUT", data: { type: "menu", roleIds: roleIds }, title: Role.title, modal: '#dialogRole', info: true });
$.bc({ id: menuId, url: Role.url, method: "put", data: { type: "menu", roleIds: roleIds }, title: Role.title, modal: '#dialogRole' });
}
},
callback: function (result) {
@ -237,7 +237,6 @@
url: Menu.iconView,
contentType: 'text/html',
dataType: 'html',
method: 'GET',
callback: function (result) {
if (result) {
$dialogIcon.find('.modal-body').html(result);

View File

@ -2,7 +2,7 @@
function loadData() {
$.bc({
url: Messages.url, method: 'GET',
url: Messages.url,
callback: function (result) {
if (result) {
$('#s_inbox').text(result.inboxCount);
@ -18,7 +18,7 @@
function listData(options) {
$.bc({
id: options.id, url: Messages.url, method: 'GET',
id: options.id, url: Messages.url,
callback: function (result) {
if (result) {
var content = result.map(function (mail) {

View File

@ -23,7 +23,7 @@
var id = $this.attr('data-id');
var result = $this.attr('data-result');
$.bc({
id: id, url: 'api/New/', method: "PUT", data: { Id: id, UserStatus: result }, title: result === "ApproveUser" ? "授权用户" : "拒绝用户",
id: id, url: 'api/New/', method: "put", data: { Id: id, UserStatus: result }, title: result === "ApproveUser" ? "授权用户" : "拒绝用户",
callback: function (result) {
if (!result) return;
$table.bootstrapTable('refresh');

View File

@ -37,12 +37,12 @@
switch ($this.attr('data-method')) {
case 'password':
data.UserStatus = 'ChangePassword';
$.bc({ url: User.url, method: "PUT", data: data, title: "更改密码" });
$.bc({ url: User.url, method: "put", data: data, title: "更改密码" });
break;
case 'user':
data.UserStatus = 'ChangeDisplayName';
$.bc({
url: User.url, method: "PUT", data: data, title: "修改用户显示名称",
url: User.url, method: "put", data: data, title: "修改用户显示名称",
callback: function (result) {
if (result) {
$('#userDisplayName').text(data.DisplayName);
@ -53,7 +53,7 @@
case 'css':
data.UserStatus = 'ChangeTheme';
$.bc({
url: User.url, method: "PUT", data: data, title: "保存样式", callback: function (result) {
url: User.url, method: "put", data: data, title: "保存样式", callback: function (result) {
if (result) {
window.setTimeout(function () { window.location.reload(true); }, 1000);
}

View File

@ -23,7 +23,7 @@
events: {
'#btn_assignUser': function (row) {
$.bc({
id: row.Id, url: User.url, data: { type: "role" },
id: row.Id, url: User.url, data: { type: "role" }, method: "post",
callback: function (result) {
var htmlTemplate = this.htmlTemplate;
var html = $.map(result, function (element, index) {
@ -39,7 +39,7 @@
},
'#btn_assignGroup': function (row) {
$.bc({
id: row.Id, url: Group.url, data: { type: "role" },
id: row.Id, url: Group.url, data: { type: "role" }, method: "post",
callback: function (result) {
var htmlTemplate = this.htmlTemplate;
var html = $.map(result, function (element, index) {
@ -55,7 +55,7 @@
},
'#btn_assignMenu': function (row) {
$.bc({
id: row.Id, url: Menu.url, data: { type: "role" },
id: row.Id, url: Menu.url, data: { type: "role" }, method: "post",
callback: function (result) {
$dialogMenuHeader.text($.format('{0}-菜单授权窗口', row.RoleName));
$btnSubmitMenu.data('type', 'menu');
@ -76,14 +76,14 @@
var userIds = $dialogUser.find(':checked').map(function (index, element) {
return $(element).val();
}).toArray().join(',');
$.bc({ id: roleId, url: User.url, method: "PUT", data: { type: "role", userIds: userIds }, modal: '#dialogUser', title: User.title, info: true });
$.bc({ id: roleId, url: User.url, method: "put", data: { type: "role", userIds: userIds }, modal: '#dialogUser', title: User.title });
},
'#btnSubmitGroup': function (row) {
var roleId = row.Id;
var groupIds = $dialogGroup.find(':checked').map(function (index, element) {
return $(element).val();
}).toArray().join(',');
$.bc({ id: roleId, url: Group.url, method: "PUT", data: { type: "role", groupIds: groupIds }, modal: '#dialogGroup', title: Group.title, info: true });
$.bc({ id: roleId, url: Group.url, method: "put", data: { type: "role", groupIds: groupIds }, modal: '#dialogGroup', title: Group.title });
},
'#btnSubmitMenu': function (row) {
var roleId = row.Id;
@ -97,7 +97,7 @@
default:
break;
}
$.bc({ id: roleId, url: Menu.url, method: "PUT", data: { type: "role", menuIds: menuIds }, modal: '#dialogMenu', title: Menu.title, info: true });
$.bc({ id: roleId, url: Menu.url, method: "put", data: { type: "role", menuIds: menuIds }, modal: '#dialogMenu', title: Menu.title });
}
}
},

View File

@ -13,7 +13,7 @@
case 'footer':
data = dataBinder.get();
$.bc({
url: Settings.url, data: { name: '网站页脚', code: data.Footer, category: Settings.title }, title: Settings.title,
url: Settings.url, data: { name: '网站页脚', code: data.Footer, category: '网站设置' }, title: '保存网站页脚', method: "post",
callback: function (result) {
if (result) $('#websiteFooter').text(data.Footer);
}
@ -22,7 +22,7 @@
case 'title':
data = dataBinder.get();
$.bc({
url: Settings.url, data: { name: '网站标题', code: data.Title, category: Settings.title }, title: Settings.title,
url: Settings.url, data: { name: '网站标题', code: data.Title, category: '网站设置' }, title: '保存网站标题', method: "post",
callback: function (result) {
if (result) $('#websiteTitle').text(data.Title);
}
@ -31,7 +31,7 @@
case 'css':
var cssDefine = $css.val();
$.bc({
url: Settings.url, data: { name: '使用样式', code: cssDefine, category: '当前样式' }, title: '网站样式',
url: Settings.url, data: { name: '使用样式', code: cssDefine, category: '当前样式' }, title: '保存网站样式', method: "post",
callback: function (result) {
if (result) {
window.setTimeout(function () { window.location.reload(true); }, 1000);
@ -50,7 +50,6 @@
$sortable.html('');
$.bc({
url: Settings.url,
method: 'GET',
callback: function (urls) {
if (urls && $.isArray(urls)) {
$.each(urls, function (index, item) {
@ -58,9 +57,7 @@
else options.url = item.Url;
$.bc({
url: options.url,
xhrFields: {
withCredentials: true
},
method: "post",
callback: function (result) {
if ($.isArray(result)) {
var html = '<div class="cache-item"><i class="fa fa-ellipsis-v"></i><div><span data-toggle="tooltip" title="{2}">{2}</span><span class="badge badge-pill badge-success">{0}</span></div><span title="{3}">{3}</span><div><span>{7}</span><button class="btn btn-danger" title="{1}" data-url="{5}?cacheKey={1}" data-toggle="tooltip" data-self="{6}" data-placement="left"><i class="fa fa-trash-o"></i></button></div></div>';
@ -83,14 +80,7 @@
});
};
var listCache = function (options) {
$.bc({
url: options.url,
xhrFields: {
withCredentials: true
}
});
};
var listCache = function (options) { $.bc({ url: options.url, method: "post" }); };
$('a[data-method]').on('click', function (e) {
e.preventDefault();
e.stopPropagation();

View File

@ -7,7 +7,7 @@
var that = $(this);
that.toggleClass('fa-spin');
$.bc({
url: Tasks.url, method: 'GET',
url: Tasks.url,
callback: function (result) {
if (result) {
var content = result.map(function (task) {

View File

@ -19,7 +19,7 @@
events: {
'#btn_assignRole': function (row) {
$.bc({
id: row.Id, url: Role.url, data: { type: "user" },
id: row.Id, url: Role.url, data: { type: "user" }, method: "post",
callback: function (result) {
var htmlTemplate = this.htmlTemplate;
var html = $.map(result, function (element, index) {
@ -35,7 +35,7 @@
},
'#btn_assignGroup': function (row) {
$.bc({
id: row.Id, url: Group.url, data: { type: "user" },
id: row.Id, url: Group.url, data: { type: "user" }, method: "post",
callback: function (result) {
var htmlTemplate = this.htmlTemplate;
var html = $.map(result, function (element, index) {
@ -54,14 +54,14 @@
var roleIds = $dialogRole.find(':checked').map(function (index, element) {
return $(element).val();
}).toArray().join(',');
$.bc({ id: userId, url: Role.url, method: 'PUT', data: { type: "user", roleIds: roleIds }, title: Role.title, info: true, modal: '#dialogRole' });
$.bc({ id: userId, url: Role.url, method: 'put', data: { type: "user", roleIds: roleIds }, title: Role.title, modal: '#dialogRole' });
},
'#btnSubmitGroup': function (row) {
var userId = row.Id;
var groupIds = $dialogGroup.find(':checked').map(function (index, element) {
return $(element).val();
}).toArray().join(',');
$.bc({ id: userId, url: Group.url, method: 'PUT', data: { type: "user", groupIds: groupIds }, title: Group.title, info: true, modal: '#dialogGroup' });
$.bc({ id: userId, url: Group.url, method: 'put', data: { type: "user", groupIds: groupIds }, title: Group.title, modal: '#dialogGroup' });
}
},
callback: function (data) {