将disqus添加到流星.js项目中

Adding disqus to a meteor.js project

本文关键字:js 项目 流星 disqus 添加      更新时间:2024-02-10

我正在尝试将disqus注释系统添加到我的应用程序中。我遵循了KLICK 这篇文章中的说明

我创建了一个名为disqus.html 的模板

 <template name="disqus">
  {{#isolate}}
    <div id="disqus_thread"></div>
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>  
  {{/isolate}}
 </template>

如果渲染了这个模板,embed.js应该加载一次,然后disqus进行重置。

Template.disqus.rendered = function() {  
  Session.set("loadDisqus", true);
  return typeof DISQUS !== "undefined" && DISQUS !== null ? DISQUS.reset({
    reload: true,
    config: function() {}
  }) : void 0;
};

对deps.autorun 中的会话更改作出反应

Meteor.startup (function () {
  Deps.autorun(function() {
    if(Session.get("loadDisqus") && !window.DISQUS) {
      var disqus_shortname = '<example>'; // required: replace example with your forum shortname
    (function() {
    var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();      
    }
  });
});

这在Firefox 25.0.1中运行良好。我可以登录、注销和创建评论。但它在Chrome 31.0.1650.57 m中不起作用。我无法登录。没有引发错误。我能做什么?有什么建议吗?

即使登录discovermetro.com/…中的disqus对我来说也是不可能的。

在这里使用会话很有趣,但没有必要。以下是我可能在你的disqus.js文件中做的事情:

var isDisqusLoaded = false,
  myScriptLoader = function funcMyScriptLoader(jsEl, callback) {
    if (window.attachEvent) {
      // for IE (sometimes it doesn't send loaded event but only complete)
      jsEl.onreadystatechange = function funcOnReadyStateChange() {
        if (jsEl.readyState === 'complete') {
          jsEl.onreadystatechange = "";
        } else if (jsEl.readyState === 'loaded') {
          jsEl.onreadystatechange = "";
        }
        if (typeof callback === 'function') {
          callback();
        }
      };
    } else {
      // most browsers
      jsEl.onload = function funcOnLoad () {
        if (typeof callback === 'function') {
          callback();
        }
      };
    }
  };
Template.disqus.rendered = function funcTplDisqusRendered() {
  if (!isDisqusLoaded) {
    var myElJs = document.createElement('script'),
      s = document.getElementsByTagName('script')[0];
    myElJs.type = 'text/javascript';
    myElJs.async = true;
    myElJs.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    myScriptLoader(myElJs, function funcEventLoaded() {
      isDisqusLoaded = true;
    });
    s.parentNode.insertBefore(myElJs, s);
  }
};

使用该脚本,您将不需要使用Deps.autorun和Session。你应该只在你想获得实时性的地方使用这种功能,但如果不需要,就避免使用,因为它会降低你的应用程序性能。

如果真的需要,你可以添加Disqus.reset,但我不确定,请查看Disqus文档。

我没有测试脚本,但应该可以。

听起来像是一个奇怪的缓存错误,你试过重置chrome的缓存吗?