简体   繁体   English

向meteor.js项目添加Disqus

[英]Adding disqus to a meteor.js project

i am trying to add the disqus commentsystem to my application. 我试图将disqus注释系统添加到我的应用程序。 I followed the instruction written in this article KLICK 我按照这篇文章写的说明KLICK

I created a template called disqus.html 我创建了一个名为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>

If this template is rendered, embed.js should load once and disqus makes a reset. 如果呈现此模板,则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;
};

React on sessionchange in deps.autorun 对deps.autorun中的sessionchange做出反应

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);
    })();      

    }
  });
});

This works fine in Firefox 25.0.1. 在Firefox 25.0.1中可以正常工作。 I can login, logout and create comments. 我可以登录,注销和创建评论。 But it isnt working in Chrome 31.0.1650.57 m. 但是它在Chrome 31.0.1650.57 m中不起作用。 It is not possible for me to login. 我无法登录。 No error is thrown. 没有引发任何错误。 What can i do? 我能做什么? Any suggestions? 有什么建议么?

Even a login to disqus in discovermeteor.com/... is not possible for me. 我什至无法登录discovermeteor.com / ...中的 disqus。

Using session here is interesting but unnecessary. 在这里使用会话很有趣,但不必要。 Here is what i migth do in your disqus.js file: 这是我在您的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);
  }
};

Using that script you won't need to use Deps.autorun and Session. 使用该脚本,您将不需要使用Deps.autorun和Session。 You should use that kind of feature only where you want to get realtime, but if don't need it, avoid it coz it will degrade your app performance. 您应该仅在想要获取实时信息的地方使用这种功能,但是如果不需要它,请避免使用它,因为它会降低应用程序的性能。

You can add Disqus.reset if it's really needed but i'm not sure, look at the disqus documentation. 如果确实需要,可以添加Disqus.reset,但是我不确定,请查看disqus文档。

I didn't test the script but it should be ok. 我没有测试脚本,但是应该没问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM