首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将disqus与emberjs集成仅适用于第一页加载。

将disqus与emberjs集成仅适用于第一页加载。
EN

Stack Overflow用户
提问于 2013-07-13 20:39:21
回答 4查看 1.2K关注 0票数 6

我正在运行成员-1.0.0-rc.5,并为disqus注释创建了一个视图,我正在传递文章ID。

我的问题是,当我从一个页面切换到另一个页面时,disqus不知道。

这是视图代码:

代码语言:javascript
复制
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);
    })();
  }
});

这个在我的车把模板里

代码语言:javascript
复制
{{view App.DisqusView identifierBinding="id"}}

因此,注释在所有页面上呈现,但是有一个注释会持续到所有页面,就好像disqus认为它们都是同一个页面一样。

我正在记录page_id和url,以确保我给出的是正确的url。

另外,当我从一个页面单击到另一个页面时,当两个页面都有disqus时,控制台会发出大量的disqus错误:

代码语言:javascript
复制
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 
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-05-11 22:15:14

集成Disqus与Ember

更新-现在有一个余烬在这里

我刚刚完成了在一个成员博客框架(见这里的资料来源)上集成异步Disqus,我就是这样做的:

首先,将选项设置为对象(所有组件都很容易访问):

代码语言:javascript
复制
App.DisqusOptions = Em.Object.create({
  shortname: 'example', // Set this to your Disqus account's shortname
});

接下来,使用它们给出的代码加载一次和一次。我是在组件中这样做的:

代码语言:javascript
复制
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方法并对其进行检查之后,将options对象上的一个setupDisqus()属性设置为true。

此外,您也可以使用script标记在应用程序模板中执行此操作,但是如果您将脚本加载到没有id of #disqus_thread的和元素的页面上,则会导致错误。

接下来,使用要在每个页面上显示注释的Em.ViewEm.Component。我们叫它App.DisqusCommentsComponent吧。此组件将没有布局(模板)。由于这个组件每次更改路由/帖子时都会加载,所以它是调用DISQUS.reset()的最佳位置。

代码语言:javascript
复制
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是为每个博客文章在控制器上设置的属性,作为控制器的模型加载。您将需要一种类似的方法来识别每一条路线,以跟踪评论。

等等,瞧!您的异步调用将在每次用户更改路由到它的模板中包含注释组件的路径时进行。例如:

代码语言:javascript
复制
// Some random hbs here for your blog post
{{disqus-comments}}

Disqus JS配置变量

无论何时设置像这些这样的配置变量,都需要在窗口上为它们设置一个属性。例如:

代码语言:javascript
复制
var disqusShortname = App.DisqusOptions.get('shortname');
window.disqus_shortname = disqusShortname;

// Do stuff with disqusShortname here

评论计数

如果您想使用Disqus的注释计数功能,可以采取类似的方法。但是,您还需要使用如下内容重新打开{{link-to}}助手调用的视图:

代码语言:javascript
复制
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:

代码语言:javascript
复制
{{#link-to 'post' this.urlString commentCount='true'}}{{/link-to}}

希望这能有所帮助。如果你有什么问题请告诉我!

票数 2
EN

Stack Overflow用户

发布于 2013-07-13 22:11:54

我已经创建了一个工作的吉斯宾,来看看。

至于我改变了什么,这句话错了一点

代码语言:javascript
复制
this.get('element').id = 'disqus_thread';

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

代码语言:javascript
复制
App.DisqusView = Ember.View.extend({
  tagName: 'div',
  elementId: 'disqus_thread',
  ...

然后用

代码语言:javascript
复制
  var page_id = this.get('elementId');

为了测试它是否正常工作,我在top的jsbin中添加了一个指向伪关于页面的链接,在“关于”页面中,您会发现一个指向索引页的链接,来回切换我没有发现任何问题,Disqus每次都会像预期的那样加载,尽管仍然会出现错误。这可能与如何将Disqus注入DOM有关。请看一看,如果对你有用,请告诉我。

希望能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2013-09-01 10:10:58

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

代码语言:javascript
复制
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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17634034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档