Skip to content

Commit a05fd90

Browse files
author
Michal Kurtak
committed
Add disqus-on-ready callback to dirDisqus
Fix for disqus unit test
1 parent c369b0f commit a05fd90

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"author": "Michael Bromley",
77
"license": "MIT",
88
"devDependencies": {
9+
"karma": "~0.12.0",
910
"bower": "^1.3.1",
1011
"grunt": "~0.4.2",
1112
"grunt-contrib-copy": "^0.5.0",

src/directives/disqus/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ on the directive's element tag. For more information on the available config var
5252
disqus-config-language="{{ post.lang }}"
5353
disqus-remote-auth-s3="{{remote_auth_s3}}"
5454
disqus-api-key="{{public_api_key}}"
55+
disqus-on-ready="ready()"
5556
ready-to-bind="{{ loaded }}"
5657
>
5758
</dir-disqus>
@@ -99,3 +100,8 @@ function myController($scope, $http) {
99100

100101
If you omit the `ready-to-bind` attribute, the Disqus widget will be created immediately. This is okay so long as
101102
you don't rely on interpolated data which is not available on page load.
103+
104+
## `disqus-on-ready` attribute
105+
106+
If Disqus is rendered, `disqus-on-ready` function will be called. Callback is registered to disqus by similar technique
107+
as explained in [this post](https://help.disqus.com/customer/portal/articles/466258-capturing-disqus-commenting-activity-via-callbacks).

src/directives/disqus/dirDisqus.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
disqus_config_language : '@disqusConfigLanguage',
3838
disqus_remote_auth_s3 : '@disqusRemoteAuthS3',
3939
disqus_api_key : '@disqusApiKey',
40+
disqus_on_ready: "&disqusOnReady",
4041
readyToBind: "@"
4142
},
4243
template: '<div id="disqus_thread"></div><a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>',
@@ -56,7 +57,7 @@
5657
isReady = "true";
5758
}
5859
if (scope.$eval(isReady)) {
59-
console.log('remote'+scope.disqus_remote_auth_s3);
60+
console.log('remote'+scope.disqus_remote_auth_s3);
6061
// put the config variables into separate global vars so that the Disqus script can see them
6162
$window.disqus_shortname = scope.disqus_shortname;
6263
$window.disqus_identifier = scope.disqus_identifier;
@@ -67,8 +68,12 @@
6768
$window.disqus_config = function () {
6869
this.language = scope.disqus_config_language;
6970
this.page.remote_auth_s3 = scope.disqus_remote_auth_s3;
70-
this.page.api_key =scope.disqus_api_key;
71-
71+
this.page.api_key = scope.disqus_api_key;
72+
if (scope.disqus_on_ready) {
73+
this.callbacks.onReady = [function () {
74+
scope.disqus_on_ready();
75+
}];
76+
}
7277
};
7378
// get the remote Disqus script and insert it into the DOM, but only if it not already loaded (as that will cause warnings)
7479
if (!$window.DISQUS) {
@@ -94,4 +99,4 @@
9499
};
95100
}]);
96101

97-
})();
102+
})();

src/directives/disqus/dirDisqus.spec.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* them on their own using `ddescribe` works okay. Therefore this test is ignored in general unless specifically testing
44
* this directive, in which case change `xdescribe` to `ddescribe`.
55
*/
6-
xdescribe('dirDisqus directive', function() {
6+
describe('dirDisqus directive', function() {
77
var scope,
88
elem,
99
compiled,
@@ -19,6 +19,7 @@ xdescribe('dirDisqus directive', function() {
1919
'disqus-category-id="{{ post.catId }}"' +
2020
'disqus-disable-mobile="false"' +
2121
'disqus-config-language="{{ post.lang }}"' +
22+
'disqus-on-ready="ready()"' +
2223
'ready-to-bind="{{ loaded }}">' +
2324
'</dir-disqus>';
2425

@@ -33,6 +34,10 @@ xdescribe('dirDisqus directive', function() {
3334
lang: 'en'
3435
};
3536
scope.loaded = false;
37+
scope.readyCalled = false;
38+
scope.ready = function() {
39+
scope.readyCalled = true;
40+
};
3641

3742
//get the jqLite or jQuery element
3843
elem = angular.element(html);
@@ -41,14 +46,20 @@ xdescribe('dirDisqus directive', function() {
4146
compiled = $compile(elem);
4247

4348
//run the compiled view.
44-
compiled(scope);
49+
var element = compiled(scope);
50+
51+
var div = document.createElement("div");
52+
div.innerHTML = element.html();
4553

46-
//call digest on the scope!
47-
scope.$digest();
54+
// Just add disqus to document - it is needed to work embed.js properly
55+
document.getElementsByTagName('body')[0].appendChild(div);
4856
});
4957
});
5058

5159
it('should not do anything when ready to bind is false', function() {
60+
//call digest on the scope!
61+
scope.$digest();
62+
5263
expect(elem.find("#disqus_thread")).toBeTruthy();
5364
expect($("script[src='//shortname.disqus.com/embed.js']").length).toEqual(0);
5465
expect(window.disqus_shortname).toBeFalsy();
@@ -57,6 +68,7 @@ xdescribe('dirDisqus directive', function() {
5768
expect(window.disqus_url).toBeFalsy();
5869
expect(window.disqus_category_id).toBeFalsy();
5970
expect(window.disqus_disable_mobile).toBeFalsy();
71+
expect(scope.readyCalled).toBeFalsy();
6072
expect(window.language).toBeFalsy();
6173
});
6274

@@ -70,7 +82,16 @@ xdescribe('dirDisqus directive', function() {
7082
expect(window.disqus_url).toEqual('http://www.test.com');
7183
expect(window.disqus_category_id).toEqual('999');
7284
expect(window.disqus_disable_mobile).toEqual('false');
85+
86+
window.page = {};
87+
window.callbacks = {};
7388
window.disqus_config();
89+
7490
expect(window.language).toEqual('en');
91+
expect(window.callbacks.onReady).toBeDefined();
92+
expect(window.callbacks.onReady.length).toEqual(1);
93+
window.callbacks.onReady[0]();
94+
expect(scope.readyCalled).toBeTruthy();
95+
7596
});
7697
});

0 commit comments

Comments
 (0)