-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Functioning socket implementation for live updating user statuses on …
…status listings page
- Loading branch information
junkafarian
committed
Apr 12, 2012
1 parent
ce43d99
commit c379b55
Showing
3 changed files
with
40 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,41 @@ | ||
var socket = io.connect('http://localhost:3003'); | ||
|
||
function update_or_append_status(username, status) { | ||
$('.status-container[rel='+username+']').each(function (i,el) {el.innerHTML = status.status; console.log(el)}); | ||
var status_template = "<p class=\"status-line\" rel=\"{{username}}\"><a href=\"/status/{{username}}\">{{username}}</a>: <span class=\"status-container\">{{status}}</span></p>" | ||
|
||
function update_or_append_status(list, username, status) { | ||
var line = list.find('.status-line[rel='+username+']'); | ||
console.log(line); | ||
if (!line.length) { | ||
// there is no line in the parent element | ||
var newline = $(status_template) | ||
.attr('rel', username) | ||
.find('a') | ||
.each(function() { | ||
$(this) | ||
.attr('href', '/status/'+username) | ||
.text(username); | ||
}) | ||
.end() | ||
.find('.status-container') | ||
.each(function() { | ||
$(this).text(status); | ||
}) | ||
.end(); | ||
|
||
newline.appendTo(list); | ||
} else { | ||
line.find('.status-container').each(function () { | ||
$(this).text(status); | ||
console.log(el); | ||
}); | ||
} | ||
} | ||
|
||
socket.on('status_update', function (data) { | ||
var to_update = document.getElementsByClassName("status-container"); | ||
for (var k in data) { | ||
var status = data[k]; | ||
update_or_append_status(status.username, status.status) | ||
} | ||
$('.status-list').each(function () { | ||
for (var k in data) { | ||
var status = data[k]; | ||
update_or_append_status($(this), status.username, status.status.status); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
<h1>Current status of all system users</h1> | ||
{{#statuses}} | ||
<p><a href="/status/{{username}}">{{username}}</a>: <span class="status-container" rel="{{username}}">{{status.status}}</span></p> | ||
{{/statuses}} | ||
<div class="status-list"> | ||
</div> |