微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单元测试 – 在单页应用程序中使用Disqus的最佳方法是什么?

在单页应用程序中使用disqus的最佳方法是什么?
我看到角度js文档已经成功地实现了它.

目前我们的方法看起来像是在AngularJS应用程序中,但似乎不稳定,很难测试,并加载错误的线程ids(同一个线程几乎随处可见).

'use strict';

angular.module('studentportalenApp.components')
    .directive('disqusComponent',['$log','$rootScope',function($log,$rootScope) {

    var _initdisqus = function _initdisqus(attrs)
    {
        if(window.disQUS) {
            disQUS.reset({
                reload: true,config: function () {
                    this.page.identifier = attrs.threadId;
                    this.disqus_container_id = 'disqus_thread';
                    this.page.url = attrs.permalinkUrl;
                }
            });
        }
        else
        {
            $log.error('window.disQUS did not exist before directive was loaded.');
        }
    }

    //Destroy disQUS bindings just before route change,to properly dispose of listeners and frame (postMessage nullpointer exception)
    $rootScope.$on('$routeChangeStart',function() {
            if(window.disQUS) {
                disQUS.reset();
            }           
    });


    var _linkFn = function link(scope,element,attrs) {
            _initdisqus(attrs);
        }


    return {
        replace: true,template: '<div id="disqus_thread"></div>',link: _linkFn
    };
}]);
我也想在我的AngularJS动力博客中加入disqus.我发现现有的解决方案有点笨重,所以我写了我自己的指令:
.directive('dirdisqus',function($window) {
    return {
        restrict: 'E',scope: {
            disqus_shortname: '@disqusShortname',disqus_identifier: '@disqusIdentifier',disqus_title: '@disqusTitle',disqus_url: '@disqusUrl',disqus_category_id: '@disqusCategoryId',disqus_disable_mobile: '@disqusdisableMobile',readyToBind: "@"
        },template: '<div id="disqus_thread"></div><a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">disqus</span></a>',link: function(scope) {

            scope.$watch("readyToBind",function(isReady) {

                // If the directive has been called without the 'ready-to-bind' attribute,we
                // set the default to "true" so that disqus will be loaded straight away.
                if ( !angular.isDefined( isReady ) ) {
                    isReady = "true";
                }
                if (scope.$eval(isReady)) {
                    // put the config variables into separate global vars so that the disqus script can see them
                    $window.disqus_shortname = scope.disqus_shortname;
                    $window.disqus_identifier = scope.disqus_identifier;
                    $window.disqus_title = scope.disqus_title;
                    $window.disqus_url = scope.disqus_url;
                    $window.disqus_category_id = scope.disqus_category_id;
                    $window.disqus_disable_mobile = scope.disqus_disable_mobile;

                    // get the remote disqus script and insert it into the DOM
                    var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
                    dsq.src = '//' + scope.disqus_shortname + '.disqus.com/embed.js';
                    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
                }
            });
        }
    };
});

优点

我认为这种方法的主要优点是它保持简单.在您的应用程序注册了该指令后,您不需要在JavaScript中编写任何JavaScript或设置任何配置值.所有配置都通过传递指令标签中的属性来处理,如下所示:

<dir-disqus disqus-shortname="YOUR_disQUS_SHORTNAME"
    disqus-identifier="{{ article.id }}"
    disqus-title="{{ article.title }}"
    ...>
</dir-disqus>

此外,您不需要更改您的index.html文件以包含disqus .js文件 – 该指令将在准备就绪时动态加载它.这意味着所有额外的.js只会在实际使用disqus指令的那些页面上加载.

您可以看到完整的源和文档here on GitHub

警告

当您的网站在HTML5Mode中时,以上内容才能正常工作,即不会在您的网址中使用“#”.我正在更新GitHub上的代码,所以在不使用HTML5Mode时,该指令将工作,但要警告您必须设置一个hashPrefix的“!”来制作“hashbang”URL. www.mysite.com/#!/page/123.这是disqus施加的限制 – 见http://help.disqus.com/customer/portal/articles/472107-using-disqus-on-ajax-sites

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中文文档更容易理解,前提是你的英语基础还可以。英文文档对于知识点讲述简明扼要,通俗易懂,而有些中文文档读起来特别费力,基础差、底子薄的有可能一会就会被绕晕了,最起码英文文档中的代码与中文文档中的代码是一致的,但知识点讲述实在是差距太大。Angular.jshasapowerfuldire
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是一个令人印象深刻的 JavaScript 图表库,建立在 HTML5 Canvas 基础上。目前,它支持6种图表类型(折线图,条形图,雷达图,饼图,柱状图和极地区域区)。而且,这是一个独立的包,不依赖第三方 JavaScript 库,小于 5KB。   其中用到的软件:   Chart.js框架,版本1.0.2,一
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,结局方案为更换jquery、angularjs、IE的版本。 1.首先尝试更换jquery版本为1.7.2 jquery-1.9.1.js-->jquery-1.7.2.js--> jquery2.1.4.js 无效 2.尝试更换IE版本IE8 IE11-
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:http://ngmodules.org/modules/angularjs-dropdown-multiselecthttp://dotansimha.github.io/angularjs-dropdown-multiselect/#/AngularJSDropdownMultiselectThisdire
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目需求,需要在首页搜索框中添加语音输入功能,考虑到科大讯飞语音业务的强大能力,遂决定使用科大讯飞语音输入第三方服务。软件首页截图如下所示: 涉及的源代码如下所示: //语音识别$rootScope.startRecognize = function() {var speech;
Angular数据更新不及时问题探讨前言 在修复控制角标正确变化过程中,发觉前端代码组织层次出现了严重问题。传递和共享数据时自己使用的是rootScope,为此造成了全局变量空间的污染。根据《AngularJs深度剖析与最佳实践》,如果两个控制器的协作存在大量的数据共享和交互可以利用Factory等服务的“单例”特性为它们注入一个共享对象来传递数据。而自己在使用rootScope
HTML:让表单、文本框只读,不可编辑的方法有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使中国">的内容,"中国"两个字不可以修改。实现的方式归纳一下,有如下几种。方法1:onfocus=this.blur()中国"onfocus=this.blur()>方法2:readonly中国"readonly>中国"readonly="tru
在AngularJS应用中实现微信认证授权遇到的坑前言 项目开发过程中,移动端新近增加了一个功能“微信授权登录”,由于自己不是负责移动端开发的,但最后他人负责的部分未达到预期效果。不能准确实现微信授权登录。最后还得靠自己做进一步的优化工作,谁让自己是负责人呢?原来负责人就是负责最后把所有的BUG解决掉。 首先,熟悉一下微信授权部分的源代码,如下所示:
AngularJS实现二维码信息的集成思路需求 实现生成的二维码包含订单详情信息。思路获取的内容数据如下: 现在可以获取到第一级数据,第二级数据data获取不到。利用第一级数据的获取方法获取不到第二级数据。for(i in data){alert(i); //获得属性 if(typeof(data[i]) == "o
Cookie'data'possiblynotsetoroverflowedbecauseitwastoolarge(5287>4096bytes)!故事起源 项目开发过程中遇到以上问题,刚开始以为只是个警告,没太在意。后来发现直接影响到了程序的执行效果。果断寻找解决方法。问题分析 根据Chrome浏览器信息定位,显示以下代码存在错误: