簡體   English   中英

向meteor.js項目添加Disqus

[英]Adding disqus to a meteor.js project

我試圖將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中的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);
    })();      

    }
  });
});

在Firefox 25.0.1中可以正常工作。 我可以登錄,注銷和創建評論。 但是它在Chrome 31.0.1650.57 m中不起作用。 我無法登錄。 沒有引發任何錯誤。 我能做什么? 有什么建議么?

我什至無法登錄discovermeteor.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的緩存?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM