6

我正在运行 ember-1.0.0-rc.5 并为我传递文章 ID 的 disqus 评论创建了一个视图。

我的问题是 disqus 不知道我何时从一页切换到另一页。

这是视图代码:

App.DisqusView = Ember.View.extend({
  tagName: 'div',
  elementId: 'disqus_thread',
  didInsertElement: function(){
    var root_url = "http://my-root-url.herokuapp.com"
    var page_id = this.get('identifier');

    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_identifier = "item-" + page_id;
    console.log(disqus_identifier);
     / this outputs the correct id/

    var disqus_title = "the song title" ;
    console.log(disqus_title);
     / this outputs the correct title/

    var disqus_url =  root_url + '/#/song/' + page_id;
    console.log(disqus_url);
     / this outputs the correct url for the page/

    var disqus_shortname = 'example'; 

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (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);
    })();
  }
});

这是在我的车把模板中:

{{view App.DisqusView identifierBinding="id"}}

因此,评论在所有页面上呈现,但一条评论持续存在于所有页面,就好像 disqus 认为它​​们都是同一个页面一样。

我正在记录 page_id 和 url 以确保我给 disqus 正确的 url。

同样,当我从一个页面单击到另一个页面时,两者都有 disqus,控制台会吐出一堆 disqus 错误:

DISQUS assertion failed: Unsafe attempt to redefine existing module: stringify [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: parse [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: domready [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: on [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: once [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: off [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: trigger [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: stopListening [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: listenTo [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: listenToOnce [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: bind [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: unbind [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: getShortnameFromUrl [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: getForum [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: isSSL [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: guessThreadTitle [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: getContrastYIQ [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: colorToHex [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: getElementStyle [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: getAnchorColor [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: normalizeFontValue [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: isSerif [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: getBrowserSupport [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: getPermalink [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: expose [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: BaseApp [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: WindowedApp [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: ThreadBoundApp [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: PublicInterfaceMixin [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: Switches [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: Profile [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: BackplaneIntegration [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: Lounge [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: Ignition [VM] embed.js (16737):1
DISQUS assertion failed: Unsafe attempt to redefine existing module: HostConfig 
4

4 回答 4

2

将 Disqus 与 Ember 集成


更新- 现在这里有一个 Ember 插件


我刚刚在 ember 博客框架上完成了异步 Disqus 的集成(请参阅此处的源代码),我就是这样做的:

首先,将选项设置为对象(所有组件都可以轻松访问):

App.DisqusOptions = Em.Object.create({
  shortname: 'example', // Set this to your Disqus account's shortname
});

接下来,使用他们提供的代码加载一次 disqus。我在组件中这样做了:

App.DisqusCommentsComponent = Em.Component.extend({
  setupDisqus: function() {
    if (!window.DISQUS) {
      var disqusShortname = App.DisqusOptions.get('shortname');
      window.disqus_shortname = disqusShortname; // Mimic behaviour as if we're setting variable in a script tag

      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqusShortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    }
  }.on('didInsertElement'),
});

您也可以通过在运行方法并检查disqusLoaded该方法后将选项对象上的属性设置为 true 来做到这一点。setupDisqus()

此外,您可以在应用程序模板中使用script标签执行此操作,但如果您在没有和元素的页面上加载脚本,这会导致错误idof #disqus_thread

接下来,使用Em.ViewEm.Component您将放置在您希望显示评论的每个页面上。让我们称之为App.DisqusCommentsComponent。该组件将没有布局(模板)。由于每次我们更改路线/帖子时都会加载此组件,因此它是调用的理想场所DISQUS.reset()

App.DisqusCommentsComponent = Em.Component.extend({
  elementId: 'disqus_thread', // ID is used by Disqus to know where to load the comments
  timer: null,

  setupDisqus: function() {
    // setupDisqus() code left out for purposes of not repeating myself
  }.on('didInsertElement'),

  loadNewPostComments: function() {
    if (window.DISQUS) {
      this.reset();
    } else {
      // If DISQUS hasn't finished async loading yet, check again in 100 ms. Once it's loaded, the above this.reset() will be called.
      this.set('timer', Em.run.debounce(this, this.loadNewPostComments, 100));
    }
  },

  reset: function() {
    var controller = this.get('parentView.controller');
    var postIdentifier = controller.get('urlString');
    var postUrl = window.location.href;

    // Since this view has the elementId Disqus targets, we need to wait until after the view has finished rendering to call the reset function, which searches for #disqus_thread
    Em.run.scheduleOnce('afterRender', function() {
      window.DISQUS.reset({
        reload: true,
        config: function () {
          this.page.identifier = postIdentifier;
          this.page.url = postUrl;
        }
      });
    });
  },
});

注意变量postIdentifier是控制器上为每篇博客文章设置的属性,作为控制器的模型加载。您将需要一种类似的方式来识别每条路线以跟踪评论。

等等,瞧!每次用户将路由更改为模板中包含评论组件的路由时,都会进行您的异步调用。例如:

// Some random hbs here for your blog post
{{disqus-comments}}

Disqus JS 配置变量

每当您设置这样的配置变量,您都需要在窗口上设置它们的属性。例如:

var disqusShortname = App.DisqusOptions.get('shortname');
window.disqus_shortname = disqusShortname;

// Do stuff with disqusShortname here

旁注:评论计数

如果您想使用 Disqus 的评论计数功能,您可以采取与上述类似的方法。但是,您还需要重新打开{{link-to}}助手调用的视图,如下所示:

Em.LinkView.reopen({

  addDisqusTag: function() {
    var commentCount = this.get('commentCount');

    if (commentCount) {
      var isLinkToPost = this.get('isLinkToPost');
      var href = this.get('href');
      var disqusTag = '#disqus_thread';

      this.set('href', href + disqusTag);
    }
  }.on('willInsertElement'),
});

现在,如果您在模板中执行以下操作,它将返回一个 commentCount:

{{#link-to 'post' this.urlString commentCount='true'}}{{/link-to}}

希望有帮助。如果您有任何问题,请告诉我!

于 2014-05-11T22:15:14.767 回答
1

我创建了一个工作jsbin,看看。

至于我改变了什么,这条线是错误的,有点错误

this.get('element').id = 'disqus_thread';

但也可以通过在视图本身上定义 elementId 来省略

App.DisqusView = Ember.View.extend({
  tagName: 'div',
  elementId: 'disqus_thread',
  ...

然后用

  var page_id = this.get('elementId');

为了测试它是否正常工作,我在顶部的 jsbin 中放置了一个指向伪 about 页面的链接,在 about 页面中,您会找到一个返回索引页面的链接,来回切换我没有看到任何问题,尽管错误仍然出现,但 Disqus 每次都按预期加载。这可能与 Disqus 如何注入到 DOM 中有关。请看一下,让我知道它是否适合您。

希望能帮助到你。

于 2013-07-13T22:11:54.197 回答
1

根据 Disqus 文档,您可以像这样重置活动线程:

DISQUS.reset({
  reload: true,
  config: function () {  
    this.page.identifier = "newidentifier";  
    this.page.url = "http://example.com/#!newthread";
  }
});

(来自http://help.disqus.com/customer/portal/articles/472107-using-disqus-on-ajax-sites

于 2013-09-01T10:10:58.437 回答
1

利用:

  • 一个组件
  • 插入元素()

组件

App.CDisqusComponent = Em.Component.extend({
    didInsertElement: function () {
        var page_id = window.location.href,
            disqus_identifier = page_id,
            disqus_url = page_id,
            disqus_title = Em.$('title').text(),
            disqus_shortname = 'disqus shortname', // CHANGE, USE YOURS
            el_id = disqus_shortname + Date.now();

        this.set('page_id', el_id);

        var dsq = document.createElement('script');
        dsq.type = 'text/javascript';
        dsq.async = true;
        dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
        dsq.id = el_id;
        (document.getElementsByTagName('head')[0] || 
         document.getElementsByTagName('body')[0]).appendChild(dsq);
    },
    willDestroyElement: function () {
        Em.$('#' +  this.get('page_id')).remove();
    }
})

组件模板

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

现在您可以在任何模板中添加 disqus:

{{c-disqus}}

分叉:https ://gist.github.com/givanse/a3b945a47438d7119989

于 2014-06-12T16:34:22.923 回答