I currently have an HTML <Table>
with all the socket.io-rooms. Client-side, the table gets fetched if you visit the page. If some room gets created or deleted, it get updated with the following code. There's also a button that gets updated to show if the room is full or not.
Now I want to know if there's any "better" way. The argument is room_list
(array with objects for each room). Should I split it into two functions (fetchRoomList
and updateRoomList
) or use "pure" JavaScript instead of jQuery? Any other ideas?
socket.on( 'updateRoomsList', function ( room_list ) {
var i;
for (i = room_list.length - 1; i >= 0; i -= 1) {
var r = room_list[ i ];
var el = $( '.room-' + r.id ).length;
var parts = [];
// Create Row
if( !el ) {
parts.push( '<tr class="room-' + r.id + '">' );
parts.push( '<td>' + r.id + '</td>' );
parts.push( '<td>' + r.name + '</td>' );
parts.push( '<td class="room-players">' + r.players.length + ' / ' +r.slots + '</td>' );
parts.push( '<td class="room-button"><a class="btn btn-block btn-success btn-xs" onclick="joinRoom(' + r.id + ');">JOIN</a></td>' );
$( '.lobby-container .room-list tbody' ).prepend( parts.join() );
}
else // Update Row
{
$( '.lobby-container .room-list tbody .room-' + r.id + ' .room-players' ).text( r.players.length + ' / ' +r.slots );
if( r.players.length >= r.slots ) { // Full Room
$( '.lobby-container .room-list tbody .room-' + r.id + ' .room-button .btn' )
.text( 'FULL' )
.addClass( 'btn-warning' )
.removeClass( 'btn-success' )
.addClass( 'disabled' );
} else { // Not full
$( '.lobby-container .room-list tbody .room-' + r.id + ' .room-button .btn' )
.text( 'JOIN' )
.addClass( 'btn-success' )
.removeClass( 'btn-warning' )
.removeClass( 'disabled' );
}
}
}
} );