Clipboard.js: Replace event emitter with promises

Created on 23 Jan 2018  路  5Comments  路  Source: zenorocha/clipboard.js

Part I of a new major release proposal #516

Current state

var clipboard = new Clipboard('.btn');

clipboard.on('success', function(e) {
  // it worked
});

clipboard.on('error', function(e) {
  // it didn't work
});

Proposal

clipboard.copy('.btn')
  .then(function(e) {
    // it worked
  })
  .catch(function(e) {
    // it didn't work
  })

Benefits

This change will make the API more modern, remove tiny-emitter dependency, and help those who need to make actions before copying (for example, make an ajax request).

Drawbacks

This change will drop support for IE: https://caniuse.com/#search=promises

Related

152 #336 #338 #361 #492

pinned

Most helpful comment

@vinhboy I think it would be better to advise people to include a promise polyfill separately if they need it

All 5 comments

Can you use bluebirdjs, or a polyfill to continue IE support?

@vinhboy I think it would be better to advise people to include a promise polyfill separately if they need it

I agree with @ClementParis016, if we add a polyfill by default the bundle will be huge. We'll probably advise people to include themselves.

I'm a little confused, am I?
a Promise is only ever resolve once..

So with this implementation if a user clicks on the button multiple times you would only ever catch the first click event?

This change will make the API more modern

I'm also confused, for the same reason as @vidhill. Promises are not just "modern" replacements for events.

and help those who need to make actions before copying (for example, make an ajax request)

Is this not already possible?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

XZZsr picture XZZsr  路  5Comments

aussiechief picture aussiechief  路  3Comments

godoffrags picture godoffrags  路  4Comments

johnymontana picture johnymontana  路  7Comments

maxjvh picture maxjvh  路  5Comments