寻找更好的方式来表达这个javascript方法
var previousZone = null;
//Evaluates whether the currently moused-over item is a RadDockZone.
//TODO: Make more understandable.
function TryGetZoneFromTarget(target) {
//Done for performance. Comparing object types is slower than a string comparison on ID.
if (target != null && target.id && target.id.indexOf("RadDockZone") != -1) {
return $find(target.id);
}
if (!target.id) {
return "IGNORE";
}
return null;
}
//Adds highlighting to the dockZones when the user is dragging objects to the screen.
//Clear the old dockZone as the user moves out of it, and color new ones as they move into it.
function OnClientDragging(sender, eventArgs) {
var target = eventArgs.get_htmlElement();
var currentZone = TryGetZoneFromTarget(target);
if (currentZone == "IGNORE") return; //When the user moves the mouse too fast inside of a zone, the zone returns no ID but this is a red-herring.
//Ignoring this prevents flickering where we temporarily remove the highlighting on a zone when not moving out of it.
if (currentZone) {
dockZoneDroppedOnID = currentZone.get_id();
if (previousZone == null) {
previousZone = currentZone;
AddHighlighting(currentZone);
}
else if (previousZone != currentZone) {
RemoveHighlighting(previousZone);
previousZone = currentZone;
AddHighlighting(currentZone);
}
}
else {
dockZoneDroppedOnID = "";
if (previousZone != null) {
RemoveHighlighting(previousZone);
previousZone = null;
}
}
}
所以,我有一个奇怪的怪癖,这使得这个方法变得更加丑陋。当客户端拖动鼠标时,如果拖动得太快,目标实际上不会返回 ID。这导致闪烁,当不穿过区域时,我会删除并重新添加突出显示。因此,我修补了这个快速修复......但它真的很糟糕。
在 Javascript 中处理这种情况的正确方法是什么?我应该枚举三种类型...“Zone”、“NotZone”、“Ignore”并从那里开始工作吗?或者...?
public class CormantRadListBox : RadListBox
{
public CormantRadListBox()
{
EnableDragAndDrop = true;
OnClientDragging = "OnClientDragging";
OnClientDropping = "OnClientDropping";
Sort = RadListBoxSort.Ascending;
Skin = "Web20";
Width = Unit.Percentage(100);
}
}
var previousZone = null;
//Evaluates whether the currently moused-over item is a RadDockZone.
//TODO: Make more understandable.
function TryGetZoneFromTarget(target) {
//Done for performance. Comparing object types is slower than a string comparison on ID.
if (target != null && target.id && target.id.indexOf("RadDockZone") != -1) {
return $find(target.id);
}
if (!target.id) {
return "IGNORE";
}
return null;
}
//Adds highlighting to the dockZones when the user is dragging objects to the screen.
//Clear the old dockZone as the user moves out of it, and color new ones as they move into it.
function OnClientDragging(sender, eventArgs) {
var target = eventArgs.get_htmlElement();
var currentZone = TryGetZoneFromTarget(target);
if (currentZone == "IGNORE") return; //When the user moves the mouse too fast inside of a zone, the zone returns no ID but this is a red-herring.
//Ignoring this prevents flickering where we temporarily remove the highlighting on a zone when not moving out of it.
if (currentZone) {
dockZoneDroppedOnID = currentZone.get_id();
if (previousZone == null) {
previousZone = currentZone;
AddHighlighting(currentZone);
}
else if (previousZone != currentZone) {
RemoveHighlighting(previousZone);
previousZone = currentZone;
AddHighlighting(currentZone);
}
}
else {
dockZoneDroppedOnID = "";
if (previousZone != null) {
RemoveHighlighting(previousZone);
previousZone = null;
}
}
}
So, I have a weird quirk which is making this method a lot uglier. When the client is dragging their mouse, if they drag too quickly, the target won't return an ID when it actually has one. This was resulting in flickering, I would remove and re-add highlighting when not moving through a zone. As such, I patched in this quick fix... but it's really bad.
What's a proper way of handling such a scenario in Javascript? Should I have an enumeration of three types... "Zone", "NotZone", "Ignore" and work from there? Or...?
public class CormantRadListBox : RadListBox
{
public CormantRadListBox()
{
EnableDragAndDrop = true;
OnClientDragging = "OnClientDragging";
OnClientDropping = "OnClientDropping";
Sort = RadListBoxSort.Ascending;
Skin = "Web20";
Width = Unit.Percentage(100);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“更好的编写方式”始终是主观的,但如果您的意思是拖动区域未定义,这与拖动区域的空/空结果不同,请检查未定义,然后检查空,然后检查值:
"A better way to write" is always subjective, but if what you mean is your drag zone is undefined, which is a different case than an empty/null result for drag zone, check for undefined, then null, then value:
id 未出现的问题可能是由于 DOM 上的事件冒泡造成的。
如果您添加如下代码:
在 mousemove 事件处理程序的开头,其中 e 是传递给处理程序的事件,它将阻止事件冒泡到没有 id 的元素,并且所有问题都会消失,除非我'我误认为丢失 ID 的原因。
Your problem with the ids not appearing is likely caused by bubbling of events up the DOM.
If you add code like:
To the start of your event handler for mousemove where e is the event passed to the handler, it will stop the event from bubbling up to elements with no id, and all your problems should go away, unless I'm mistaken by the cause of the missing ids.