0

So I'm working on a project from a book where TaffyDB is used to create a collection of people. The project is a simple chatroom, where the list of users is generated as an html list of online users in the window. Currently I'm using fake data to test, and I have one for each loop (in jquery) that works correctly, printing five "avatars" to the screen.Yet, my each loop to print the online users runs twice: why is this? These two loops use the exact same setup, yet the second runs twice.

The for each that works:

people_db().each(function(person,idx){
        var class_list;
        if( person.get_is_anon() ){ return true; }
        class_list = ['spa-avtr-box'];

        if( person.id === chatee.id ){
            class_list.push('spa-x-is-user');
        }

        $box = $('<div/>')
            .addClass(class_list.join(' '))
            .css( person.css_map )
            .attr('data-id', String(person.id) )
            .prop( 'title', spa.util_b.encodeHtml(person.name) )
            .text( person.name )
            .appendTo( $nav );
    });

The loop that runs twice:

people_db().each(function(person,idx){

        var select_class = '';

        console.log(person);

        if( person.get_is_anon() || person.get_is_user() ) {
            return true;
        }

        if( chatee && chatee.id === person.id ){
            select_class = 'spa-x-select';
        }

        list_html
            += '<div class="spa-chat-list-name'
            + select_class + '" data-id="' + person.id + '">'
            + spa.util_b.encodeHtml(person.name) + '</div>';
    });

    if( !list_html ){
        list_html = String()
        + '<div class="spa-chat-list-note">'
        + 'To chat alone is the fate of all great souls...<br><br>'
        + 'No one is online'
        + '</div>';

        clearChat();
    }
    jqueryMap.$list_box.html(list_html);
}

Any ideas what I'm doing wrong? I've gone through the rest of my code extensively and I'm not calling it twice, I'm almost certain is has to be something with the loop.

4

1 回答 1

0

我首先要确保你没有调用它两次。如果您查看控制台,是否

console.log(person);

打印两次?如果是这样,请解除绑定任何呼叫

people_db().each(function(person,idx){

在你绑定它之前。

于 2015-02-23T18:19:40.743 回答