Technical Blog

coComment JSONP Native

Posted on

coComment has added native JSONP support. They seem to have worked out the bugs from their first implementation and I have reworked the code for my sidebar to use their system. Out of this has come two generic coCo JSON functions. One, coco_flat, takes their nested group-comments-by-article format and flattens it to one-element-per-comment with all the article and comment attributes intact:

function coco_flat(coco) {
var comments = [];
for(var i=0; i<coco.length; i++) {
for(var i2=0; i2<coco[i].comments.length; i2++) {
}//end for cocommentResultSet[i].comments
}//end for cocommentResultSet
return comments;
}//end function coco_flat

The other, coco_compare, is for use in sorting so flattened results by date:

function coco_compare(a,b) {
if(< return -1;
if(> return 1;
return 0;
}//end function coco_compare

To use this function to sort a flattened array of coComment data oldest to newest call array.sort(coco_compare), and to sort by newest to oldest also call array.reverse() as well.

Here is an example piece of code putting together all that is above, an incidentally what I am using in my sidebar:

function list_cocomment(blockid,username,charlimit,itemlimit,boxid) {
if(!charlimit) limit = 25;
if(!itemlimit) limit = 10;
if(!boxid) boxid = 0;
coco_callback = function(data) {
var block = document.getElementById(blockid);
var txt = ‘<ul class=”cocomment”>’;
comments = coco_flat(data);
var elipsis = ”;
for(var i=0; i<comments.length && i<itemlimit; i++) {
elipsis = ”;
if(comments[i].comment.length > charlimit)
elipsis = ‘…’;
txt += ‘<li><a href=”‘+comments[i].articleURL+'” title=”Coment on ‘+comments[i].articleTitle+’ by ‘+comments[i].author+'”>’+striphtml(comments[i].comment).substr(0,charlimit)+elipsis+'</a> <a href=”‘+comments[i].articleURLCoco+'”><img src=” alt=”[co]” /></a></li>’;
}//end for comments
txt += ‘</ul>’;
txt += ‘<a href=” src=” alt=”More &raquo;” /></a> ‘;
txt += ‘<a href=” src=” alt=”[feed]” /></a><br />’;
block.innerHTML = txt;
}//end function-var coco_callback
}//end function list_cocomment

One Response

Johan Sundström

Might I again suggest lining up the icons (coComment this time, rather than plus signs) in the list on the left side rather than on the right? An unbroken column is a lot easier to find and click than one that you have to track in sideways movements, especially using a mouse or a trackball.

Leave a Response