开发者

Extra parameter in .trigger() does not reach the listener function: what is happening?

I am tinkering with this code:

var items;

function getMessage(index) {
    if (index == items.size()) return;
    var item = items.eq(index);
    $.get('/message/' + index,
        function(response) {
            item.html(response);
            $("ul").trigger('updated', index);
        }
    );
}

$(document).ready(function(){
    items = $(".message");

    $("ul").bind('updated',
       开发者_如何学编程 function(event, i) {
            getMessage(i + 1);
        }
    );
    getMessage(0);

});

And this html:

<ul>
    <li class="message"></li>
    <li class="message"></li>
    <li class="message"></li>
</ul>

According to the documentation of the jQuery .trigger() function,

"A single [extra] parameter can be passed without using an array."

But in the above code, the function listening on the "update" event never gets the parameter "index" (passed in line 9), unless I enclose it in brackets like this:

$("ul").trigger('updated', [index]);

I am not even sure if this is something related specifically to jQuery. Could you help me understand what is happening?


+1 Nash

Doesn't looks like if jQuery isn't handling the case of one parameter passed without an Array. Verified both from the API & the latest source.

The only exception i m guessing is that the value is received by a variable called data. And it goes like this:

data ? jQuery.makeArray( data ) : [];

Now in JavaScript, if the data variable has value 0 (number), the if check returns false. So, when the first time you would run it, it will not execute makeArray().

Again, this is only a think-aloud guess. Please don't get misguided, if that is not the case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜