Skip to content

Instantly share code, notes, and snippets.

@mako-taco
Last active June 28, 2018 07:34
Show Gist options
  • Save mako-taco/8eb42efb9a2a34f9e283 to your computer and use it in GitHub Desktop.
Save mako-taco/8eb42efb9a2a34f9e283 to your computer and use it in GitHub Desktop.
Single page app addthis api

#API for single page app use

###addthis.layers.refresh()

  • Updates the share URL of all floating tools (sharing sidebar, for instance) to the current URL
  • Creates inline tools on any elements that do not yet have a tool rendered for it

I can see someone with a single page app doing one of two scenarios:

//always refresh on URL change
window.addEventListener("hashchange", function () {
  addthis.layers.refresh();
});

//refresh manually
someFramework.loadPage(function () {
  addthis.layers.refresh();
})

####Use case with multiple sets of share buttons Often, a single 'page' will have multiple sets of sharing buttons on it. For example, a news site might use infinite scroll to append new articles to the bottom of a page when a user scrolls that far down. In these cases, it's important for each set of share buttons to share a different URL.

To support this, simply include data-url and data-title attributes on any new addthis containers you append to the page. Once the container has been added to the DOM with these attributes, call addthis.layers.refresh().

#####Before:

<!-- share buttons already loaded -->
<article>
  <div class="addthis_responsive_sharing" 
    data-url="/cool-article"
    data-title"Super Cool Article">
    <div>
      <a class="twitter">...</a>
      <a class="facebook">...</a>
      <a class="addthis">...</a>
    </div>
  </div>
</article>

#####After appending new content:

<article>
  <div class="addthis_responsive_sharing" 
    data-url="/cool-article"
    data-title"Super Cool Article">
    <div>
      <a class="twitter">...</a>
      <a class="facebook">...</a>
      <a class="addthis">...</a>
    </div>
  </div>
</article>
<!-- a new article, with a new div to render share buttons to -->
<article>
  <div class="addthis_responsive_sharing" 
    data-url="/newer-article"
    data-title="Newly Appended Article">
  </div>
</article>

A call to addthis.layers.refresh would transform the div with class addthis_responsive_sharing in to a new group of responsive sharing buttons, with it's own URL to share.

As with any inline element, you need to get the appropriate HTML snippet from the dashboard, so we know which elements on your page to render to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment