所有文章都加载相同的disqus线程

时间:2014-02-20 09:43:28

标签: javascript knockout.js durandal disqus

我有使用durandaljs和knockoutjs的SPA页面。现在我将为我的网站实现disqus插件的支持。

当我添加disqus时,我发现相同的评论显示在不同的网页上(我使用durandal进行网址重写)。

我想这个问题的根源是我的重写URL被完整的hashbang(#!)区分开来 enter image description here

代码如下:

    var disqus_shortname = 'somename';
    var disqus_identifier = context.id;

    (function () {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    if (typeof DISQUS != 'undefined') {
        DISQUS.reset({
            reload: true,
            config: function () {
                this.page.title = "title" + context.id;
                this.page.identifier = context.id;
                this.page.url = "http://localhost:23054/#!ideas/details/" + context.id;
                debugger;
            }
        });
    }

查看:

<div id="disqus_thread"></div>

我希望有人在SPA页面上使用disqus评论并知道如何帮助我解决这个问题。

2 个答案:

答案 0 :(得分:3)

这是我的解决方案:

    var id = context.id;
    disqus_shortname = 'somename',
    disqus_identifier = id,
    disqus_title = id,
    disqus_url = "http://someurl.com/#!" + id;
        if ($('head script[src="http://' + disqus_shortname + '.disqus.com/embed.js"]').length == 0) {
            (function () {
                var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
                dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
                (document.getElementsByTagName('head')[0]).appendChild(dsq);
            })();
        }
        if (typeof DISQUS != "undefined") {
            DISQUS.reset({
                reload: true,
                config: function () {
                    this.page.identifier = id;
                    this.page.url = "http://someurl.com/#!" + id;
                }
            });
        }

答案 1 :(得分:1)

我认为问题可能是您的标识符不够独特。来自Disqus文档:

  

当访问Disqus启用Disqus页面时,Disqus使用此标识符   确定要加载的相应注释线程。如果合适的话   无法找到线程,创建了一个新线程。

我不确定context.id是什么,但请确保这对每个帖子都是唯一的,并且对于Disqus来说足够独特。