home
  • Blog
1.12
  • Getting Started
  • Concepts
  • The Object Model
  • Application
  • Templates
  • Routing
  • Components
  • Controllers
  • Models
  • Views
  • Enumerables
  • Testing
  • Configuring Ember.js
  • Ember Inspector
  • Cookbook
    • Introduction
    • Contributing
    • User Interface and Interaction
    • Event Handling & Data Binding
    • Helpers & Components
      • Introduction
      • Creating Reusable Social Share Buttons
      • A Spinning Button for Asynchronous Actions
      • Adding Google Analytics Tracking
    • Working with Objects
  • Understanding Ember.js
  • Contributing to Ember.js
Old Guides - You are viewing the guides for Ember v1.12.0.
Go to v5.0.0

Creating Reusable Social Share Buttons

Edit pencil

Problem

You want to create a reusable Tweet button for your application.

Solution

Write a custom component that renders the Tweet button with specific attributes passed in.

{{share-twitter class='twitter-share-button' href=url
                    data-text=text
                    data-size="large"
                    data-hashtags="emberjs"}}
app/components/share-twitter.js
export default Ember.Component.extend({
  tagName: 'a',
  classNames: 'twitter-share-button',
  attributeBindings: ['data-size', 'data-url', 'data-text', 'data-hashtags']
});

Include Twitter's widget code in your HTML:

<script>
window.twttr=(function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],t=window.twttr||{};if(d.getElementById(id))return;js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);t._e=[];t.ready=function(f){t._e.push(f);};return t;}(document,"script","twitter-wjs"));
</script>

Note: the Twitter api does change from time to time. Refer to the documents if necessary.

Discussion

Twitter's widget library expects to find an <a> tag on the page with specific data- attributes applied. It takes the values of these attributes and, when the <a> tag is clicked, opens an iFrame for twitter sharing.

The share-twitter component takes four options that match the four attributes for the resulting <a> tag: data-url, data-text, data-size, data-hashtags. These options and their values become properties on the component object.

The component defines certain attributes of its HTML representation as bound to properties of the object through its attributeBindings property. When the values of these properties change, the component's HTML element's attributes will be updated to match the new values.

An appropriate tag and css class are applied through the tagName and classNames properties.

left arrow
Introduction
A Spinning Button for Asynchronous Actions
right arrow
On this page

  • Problem
  • Solution
  • Discussion
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2023 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue