:
// index.html:
<script>
(function() {
var dsq = document.createElement('div');
dsq.setAttribute('id', 'disqus_thread');
dsq.style.display = 'none';
(document.head || document.body).appendChild(dsq);
// The script below looks for a div with id disqus_thread when it runs.
// By creating the div above, we prevent the following error:
// Uncaught TypeError: Cannot read property 'appendChild' of null
var s = document.createElement('script');
s.src = '//example.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(document.head || document.body).appendChild(s);
})();
</script>
次に、私たちが作成したdiv要素を削除します。私たちは、個々のブログ投稿ごとにDisqusをリセットする準備ができたら、もう一度作成します。
// post.html
<template>
<require from="path-to-custom-element/disqus"></require>
...
// The view-model for this view should expose an object that contains
// the blog post information required by Disqus. The custom element should get
// access to this view's binding context, as shown above.
<disqus></disqus>
</template>
:私たちは私たちのブログの記事をロードしているところ
// disqus.html
<template>
<div id="disqus_thread"></div>
</template>
// disqus.js
import {bindable, customElement} from 'aurelia-framework';
@customElement('disqus')
export class Disqus {
@bindable post;
bind(bindingContext) {
// Making sure the parent view-model exposes an object that contains the information
// needed by Disqus.
// This will trigger the function below.
this.post = bindingContext.post;
}
// Conventional method that gets called when the bindable property post changes.
postChanged(newPost, oldPost) { // oldPost is null in this case
DISQUS.reset({
reload: true,
config: function() {
this.page.identifier = newPost.identifier;
// For some reason, urls with # don't work with Disqus.
// Working url: http://example.com/blog/example-post
// Non working url: http://example.com/#/blog/example-post
this.page.url = 'http://example.com' + newPost.subdirectory;
this.page.title = newPost.title;
}
});
}
}
最後には、私たちはビューでディスカスタグを配置します。
// app.js
export class App {
...
attached() {
let dsq = document.getElementById('disqus_thread');
dsq.parentNode.removeChild(dsq);
}
}
はその後、我々はディスカスのカスタム要素を作成します