Singpolyma

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++) {
comments.push({
“blogID”:coco[i].blogID,
“blogTitle”:coco[i].blogTitle,
“blogURL”:coco[i].blogURL,
“blogURLCoco”:coco[i].blogURLCoco,
“articleID”:coco[i].articleID,
“articleTitle”:coco[i].articleTitle,
“articleURL”:coco[i].articleURL,
“articleURLCoco”:coco[i].articleURLCoco,
“id”:coco[i].comments[i2].id,
“author”:coco[i].comments[i2].author,
“authorURL”:coco[i].comments[i2].authorURL,
“authorAlias”:coco[i].comments[i2].authorAlias,
“comment”:coco[i].comments[i2].comment,
“commentURLCoco”:coco[i].comments[i2].commentURLCoco,
“dateISO”:coco[i].comments[i2].dateISO,
“date”:coco[i].comments[i2].date
});
}//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(a.date<b.date) return -1;
if(a.date>b.date) 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);
comments.sort(coco_compare);
comments.reverse();
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=”http://www.cocomment.com/images/logo-16 alt=”[co]” /></a></li>’;
}//end for comments
txt += ‘</ul>’;
txt += ‘<a href=”http://www.cocomment.com/comments src=”http://www.cocomment.com/dyn/images/buttons alt=”More &raquo;” /></a> ‘;
txt += ‘<a href=”http://www.cocomment.com/mybox-rss2 src=”http://wrinks.ning.com/feedicon12x12.png alt=”[feed]” /></a><br />’;
block.innerHTML = txt;
}//end function-var coco_callback
writeScript(‘http://www.cocomment.com/mybox-json?nick=singpolyma&box=1&callback=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