寻找更好的方式来表达这个javascript方法

时间:2011-08-15 22:35:14

标签: javascript refactoring

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时就不会返回ID。这导致闪烁,我会删除并重新添加突出显示不通过区域时。因此,我修补了这个快速解决方案......但这真的很糟糕。

在Javascript中处理这种情况的正确方法是什么?我应该列举三种类型......“区域”,“NotZone”,“忽略”并从那里开始工作?要么...?

public class CormantRadListBox : RadListBox
{
    public CormantRadListBox()
    {
        EnableDragAndDrop = true;
        OnClientDragging = "OnClientDragging";
        OnClientDropping = "OnClientDropping";
        Sort = RadListBoxSort.Ascending;
        Skin = "Web20";
        Width = Unit.Percentage(100);
    }
}

2 个答案:

答案 0 :(得分:1)

“更好的写法”总是主观的,但是如果你的意思是你的拖拽区是未定义的,这与拖拽区的空/ null结果不同,检查未定义,然后是null,然后是值:

function TryGetZoneFromTarget(target) { 

     if(/\S/.test(e.target.id) == false){
           return undefined;
     }

     var c = $find(e.target.id);

     if(c && Telerik.Web.UI.RadDockZone.isInstanceOfType(c)) {
        return c;
     }

     return null;
}

function OnClientDragging(sender, eventArgs) {       
     var target = eventArgs.get_htmlElement();       
     var currentZone = TryGetZoneFromTarget(target);   

     if(currentZone === undefined) {
           // return or do nothing
     } else if(currentZone === null) {
           // do something 
     } else {
           // do something else
     }
}

答案 1 :(得分:0)

你的问题没有出现,可能是由于DOM上的事件冒泡造成的。

如果您添加以下代码:

if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();

对于mousemove的事件处理程序的开始,其中e是传递给处理程序的事件,它将阻止事件冒泡到没有id的元素,并且所有问题都应该消失,除非我错了失踪ids的原因。