如何将 Twitter(带有 JS 和所有内容)共享按钮嵌入到 Mustache 模板中?
我遇到的问题是我的 AJAX 应用程序不会重新加载页面,而只是根据 Mustache 模板切换 View 。我注意到 widjets.js 提供的功能根本没有打开,因为 JS 文件在每个应用程序生命周期中仅加载一次,并在 DOM_READY 上搜索用“twitter”标签装饰的标签。然而,这完全排除了稍后从模板渲染 HTML 的情况。
我知道我可以使用 Twitter 的原始超链接并将其自定义为看起来像一个按钮,但这太原始了。
最佳答案
这是一个对我有用的解决方案,使用 HTML 文件中的模板:
social.js:
$(function() {
var socialFeeds = {
twitter: {
account: "your-twitter-account",
script: function() {
// twitter code goes here, minus the script tags
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
// in my case, I had to re-render the twitter button, see here:
// http://stackoverflow.com/a/6536108/805003
$.ajax({ url: 'http://platform.twitter.com/widgets.js', dataType: 'script', cache:true});
}
}
}
$.Mustache.addFromDom('social-template');
$('#social').mustache('social-template', socialFeeds);
}
index.html:
<script id="social-template" type="text/html">
<div id='social'>
<p>Twitter button:</p>
<a href="https://twitter.com/share" class="twitter-share-button" data-via="{{twitter.account}}">Tweet</a>
{{ twitter.script }}
</div>
</script>
关于javascript - 将 Twitter 分享按钮(带有 JS 和所有内容)嵌入到 Mustache 模板中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9736425/