Skip to content

Commit

Permalink
Fix display of output buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
embhd committed Apr 7, 2018
1 parent 612f8fc commit 4d9b27c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions htdocs/js/mpd.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,15 @@ function webSocketConnect() {
break;
case 'outputnames':
$('#btn-outputs-block button').remove();
if (obj.data.length > 1) {
$.each(obj.data, function(id, name){
var btn = $('<button id="btnoutput'+id+'" class="btn btn-default" onclick="toggleoutput(this, '+id+')"><span class="glyphicon glyphicon-volume-up"></span> '+name+'</button>');
btn.appendTo($('#btn-outputs-block'));
});
} else {
$('#btn-outputs-block').addClass('hide');
}
var ttl_output = 0;
$.each(obj.data, function(id, name){
var btn = $('<button id="btnoutput' + id +
'" class="btn btn-default" onclick="toggleoutput(this, ' + id + ')">' +
'<span class="glyphicon glyphicon-volume-up"></span> ' + name + '</button>');
btn.appendTo($('#btn-outputs-block'));
ttl_output++;
});
if (ttl_output == 1) { $('#btn-outputs-block').addClass('hide'); }
/* remove cache, since the buttons have been recreated */
last_outputs = '';
break;
Expand Down

3 comments on commit 4d9b27c

@SuperBFG7
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, that doesn't work.
obj.data.length is undefined, you have to use Object.keys(obj.data).length

@embhd
Copy link
Owner Author

@embhd embhd commented on 4d9b27c Apr 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I figured the same after Issue notandy#170, hence the new pull request.

I'm wondering though, which solution would be faster? I suppose .keys() would loop over Object to gather all keys, before .length() would loop over the resulting array to count the number of entries, thus - depending on the result - leading to a total of two or three loops. The proposed way, on the other hand, loops only once, but changes the DOM even if it's not necessary after all.

@SuperBFG7
Copy link

@SuperBFG7 SuperBFG7 commented on 4d9b27c Apr 30, 2018 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.