javascript - 将 Twitter 分享按钮(带有 JS 和所有内容)嵌入到 Mustache 模板中

标签 javascript dom twitter mustache

如何将 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/

相关文章:

javascript - window.location.href 改变时的事件

javascript - 通过单击使用普通 JavaScript 来删除动态创建的元素

python - Twitter 流媒体 : Get trends on Twitter on the basis of countries

javascript - 如何为动态添加的新div添加选项

javascript - KonvaJS 过滤器值的等价物

javascript - 如何在窗口大小调整时使用 Jquery 从 DOM 添加/删除 Div

java - 使用 Java 中的 twitter api 将超过 140 个字符的链接发布到 twitter

javascript - 无需身份验证即可获取 JSON 形式的 Twitter Feed

javascript - 具有动态值的 Angular 形式验证消息

javascript - 如何使用 Nv.d3.js 库将饼图置于顶部?