Неожиданно передать целое число по ссылке? - PullRequest
1 голос
/ 25 марта 2009

У меня есть некоторые проблемы с этим. По какой-то причине updateCurrentItem всегда вызывается с одним и тем же параметром независимо от

function updatePromoList(query) {
    query = query.toUpperCase();
    clearCurrentList();
    var numItems = 0;
    currentItem = 0;

    result_set = cached[query.charAt(0)][query.charAt(1)][query.charAt(2)];

    for(i in result_set){
        if(numItems >= NUMBER_MATCHES){
            $("<li/>").html("<span style='color: #aaa'>Please try to limit your search results</span>").appendTo("#possibilities").mouseover(function(event){ updateCurrentItem(numItems+1); });
            break;
        }

        promo = result_set[i];
        found_number = false;
        if (!promo.client)
            found_number = (promo.prj_number.toString().substr(0,query.length) == query) ? true : false;

        if (query.length >= MATCH_NAME) {
            if(promo.prj_name && typeof promo.prj_name == 'string'){
                found_name = promo.prj_name.toUpperCase().indexOf(query);
            } else {
                found_name = -1;
            }  
            if (promo.client)
                found_client = promo.client_name.toString().indexOf(query);
            else
                found_client = -1;
        } else {
            found_name = -1;
            found_client = -1;
        }

        if(found_client >= 0) {
            var thisIndex = numItems+1;
            console.log("setting", thisIndex);
            $("<li/>").text(promo.client_name).bind('click',function(e){ updatePromoChoice(e,promo); }).appendTo("#possibilities").mouseover(function(event){ updateCurrentItem(thisIndex); });        } else if(found_name >= 0 || found_number) {            var thisIndex = numItems+1;
            console.log("setting", thisIndex);
            $("<li/>").text(promo.prj_number+": "+promo.prj_name).bind('click',function(e){ updatePromoChoice(e,promo); }).appendTo("#possibilities").mouseover(function(event){ updateCurrentItem(thisIndex); });
        }

        if(found_number || found_client >= 0 || found_name >= 0){
            numItems++;
        }
    }
}


function updateCurrentItem(i){
    currentItem = i;
    console.log("updating to", i);
}

Результаты выполнения этого:

setting 0
setting 1
setting 2
setting 3
setting 4
setting 5
setting 6
setting 7
setting 8
setting 9
setting 10
setting 11
setting 12
setting 13

затем, когда я перемещаю свою мышь над областью содержимого, содержащей эти <li> с событиями mouseOver, все, что я когда-либо вижу, это:

updating to 4

Всегда 4. Есть идеи?

1 Ответ

2 голосов
/ 25 марта 2009

Вы создаете замыкание, но оно по-прежнему связано с переменной numItems:

function(event){ updateCurrentItem(numItems+1); }

Что вы должны сделать, это что-то вроде этого:

(function(numItems){return function(event){ updateCurrentItem(numItems+1); }})(numItems)

Редактировать: Я думаю, что у меня может быть неправильная функция, но применяется тот же принцип:

function(event){ updateCurrentItem(thisIndex); }

должно быть

(function(thisIndex)
{
    return function(event){ updateCurrentItem(thisIndex); }
})(thisIndex)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...