Integrating disqus with emberjs only works when loading the first page

I run ember-1.0.0-rc.5 and created a view for disqus comments to which I pass the article id.

My problem is that disqus does not know when I switch from one page to another.

This is a code of the form:

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); })(); } }); 

and this is in my steering pattern:

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

Thus, comments are rendered on all pages, but one comment is saved on all pages, as if disqus believes that they are all one page.

I register page_id and url to make sure that I give disqus the correct URL.

also, when I click one page on the other, when both have disqus, the console spits out a bunch of disqus errors:

 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 
+6
source share
4 answers

Disqus Integration with Ember


Update - now here is Ember Addon .


I just finished integrating async Disqus into ember blogging frameworks ( see source here ), and here is how I did it:

First set the parameters as objects (easily accessible for all components):

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

Then download disqus once and only once using the code that they give you. I did this in the component:

 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'), }); 

You can also do this by setting the disqusLoaded property of the options property to true after running the setupDisqus() method and checking for it.

In addition, you can do this in your application template using script tags, but this will result in an error if you load the script on a page that does not exist, and an element with id #disqus_thread .

Then use Em.View or Em.Component , which you place on every page where you want comments to be displayed. Let me call it App.DisqusCommentsComponent . This component will not have a layout (template). Since this component will load every time we change routes / messages, this is an ideal place to call 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 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; } }); }); }, }); 

NB The postIdentifier variable is the set of properties on the controller for each blog post that is downloaded as the controller model. You will need a similar way to identify each route to track comments.

Et voila! Your asynchronous call will be executed every time a user changes routes to one that has a comment component in its template. For instance:

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

JS Disk Configuration Variables

Whenever you set a configuration variable, such as these , you need to set their property in the window. For instance:

 var disqusShortname = App.DisqusOptions.get('shortname'); window.disqus_shortname = disqusShortname; // Do stuff with disqusShortname here 

Sidenote: Number of Comments

If you want to use the Disqus comment count function, you can take a similar approach to the above. However, you will also need to re-open the {{link-to}} sub-call view using the following:

 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'), }); 

Now, if you do the following in your template, it will return commentCount:

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

Hope this helps. Let me know if you have any questions!

+2
source

I created a working jsbin , look.

As for what I changed, this line is erroneously mistaken

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

but can also be omitted by defining elementId on the view itself with

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

And then retrieved using

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

To check that it works, I put jsbin on the top link on the pseudo page, on the about page you will find the link to the index page, switching back and forth I see no problems, Disqus loads every time as expected, although the errors are still appear. This may be due to the way Disqus is introduced into the DOM. Please take a look and let me know if it works for you.

Hope this helps.

+1
source

According to the Disqus docs, you can reset use the active thread as follows:

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

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

+1
source

Using:

  • Component
  • didInsertElement ()

Component

 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(); } }) 

Component Template

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

Now you can add disqus to any template with:

 {{c-disqus}} 

fork it: https://gist.github.com/givanse/a3b945a47438d7119989

+1
source

Source: https://habr.com/ru/post/949383/


All Articles