重构代码:优化消息拉取逻辑,resetWidget根据新dropdown-menu结构重写,改名为clearWidgetItems

This commit is contained in:
Argo-MacBookPro 2018-09-06 15:47:07 +08:00
parent e5c73cedf1
commit cefcaabd4b
1 changed files with 14 additions and 23 deletions

View File

@ -34,24 +34,16 @@
} }
}); });
}, },
resetWidget: function () { clearWidgetItems: function () {
var widgets = $(this).children('li'); this.children('.dropdown').each(function () {
widgets.each(function () { $(this).children('.dropdown-menu').each(function () {
var widget = $(this).children('ul'); $(this).children('a').remove();
if (widget.children().length === 3) return;
var last = widget.children(':last');
while (widget.children().length > 3) {
widget.children(':eq(2)').remove();
}
}); });
return $(this);
}
}); });
return this;
$.extend({ },
pullNotification: function (nav) { pullNotification: function () {
if (nav.length === 0) return; var that = this;
var uri = "ws://" + window.location.host + $.formatUrl("WS"); var uri = "ws://" + window.location.host + $.formatUrl("WS");
var socket = new WebSocket(uri); var socket = new WebSocket(uri);
socket.onmessage = function (e) { socket.onmessage = function (e) {
@ -68,11 +60,10 @@
break; break;
} }
} }
if (result.length > 0) nav.reloadWidget(); if (result.length > 0) that.reloadWidget();
}; };
} return this;
}); },
$.fn.extend({
reloadWidget: function () { reloadWidget: function () {
if (this.length === 0) return this; if (this.length === 0) return this;
var that = this; var that = this;
@ -82,7 +73,7 @@
method: 'GET', method: 'GET',
callback: function (result) { callback: function (result) {
$('#logoutNoti').text(result.NewUsersCount === 0 ? "" : result.NewUsersCount); $('#logoutNoti').text(result.NewUsersCount === 0 ? "" : result.NewUsersCount);
that.resetWidget(); that.clearWidgetItems();
// tasks // tasks
$('#msgHeaderTask').text(result.TasksCount); $('#msgHeaderTask').text(result.TasksCount);
$('#msgHeaderTaskBadge').text(result.TasksCount === 0 ? "" : result.TasksCount); $('#msgHeaderTaskBadge').text(result.TasksCount === 0 ? "" : result.TasksCount);
@ -203,5 +194,5 @@ $(function () {
$('[data-toggle="dropdown"].dropdown-select').dropdown('select'); $('[data-toggle="dropdown"].dropdown-select').dropdown('select');
// load widget data // load widget data
$.pullNotification($('.header .nav').reloadWidget()); $('.header .nav').reloadWidget().pullNotification();
}); });