Archive pour août 2012

OnNewComment callback for Disqus 2012

Samedi 18 août 2012

PunchTab supports Disqus comments to give points and badges when users post a message. Their recommended solution for this is using:

function disqus_config() {
    this.callbacks.onNewComment = [function() {
        trackComment();
    }];
}

This solution works only if you define disqus_config before loading Disqus. In our case, we have no guaranty. So we ended up using a more reliable solution which works in both cases:

if (window.DISQUS.bind) {
    window.DISQUS.bind('comment.onCreate', function() {
        PT.event.trigger("main.comment", {})
    });
}

But with the new Disqus 2012 product, this was not working anymore. They still support the onNewComment callback, which works if PunchTab loads before Disqus, but the bind solution was broken if PunchTab loads after. We’ve finally found a solution for it:

var app;
if (window.DISQUS.App) {
    app = window.DISQUS.App.get(0);
    if (app) {
        app.bind('posts.create', function(message) {
            PT.event.trigger("main.comment", message)
        });
    }
}

This is not documented so it can break at any time. We’re in touch with the Disqus team to see if there is a better way to achieve this.