question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Destroying and recreating the dropin

See original GitHub issue

I think you guys should expose a drop-in API for destroying the thing, cause as it is, we have to do it ourselves and it can be a pain, since simply emptying the container will keep the event handler bound to the purchase button, meaning the requestPaymentMethod will be called multiple times.

This is how I used to do it (which was buggy since it would trigger the requestPaymentMethod multiple times upon re-creating it):

    createDropin : function() {
        var self = this;

        // Braintree
        var submitButton = document.querySelector('#braintree-submit-button');

        braintree.dropin.create({
            authorization: 'xxx', // @TODO change
            selector: '#braintree-dropin',
            paypal: {
                flow: 'checkout',
                amount : this.getMoney(),
                currency: 'EUR'
            }
        }, function (err, dropinInstance) {
            if (err) {
                // Handle any errors that might've occurred when creating Drop-in
                console.error(err);
                return;
            }

            submitButton.addEventListener('click', function () {
                dropinInstance.requestPaymentMethod(function (err, payload) {
                    if (err) {
                        // Handle errors in requesting payment method

                        //self.displayResponse(err, 'bg-danger');
                        return;
                    }

                    // Send payload.nonce to your server
                    self.sendPayload(payload);
                });
            });
        });
    },

    destroyDropin : function() {
        $('#braintree-dropin').empty();
    },

Now I do it like this, which works almost perfectly:


    _submitButton : null,
    _submitButtonEventCallback : null,

    createDropin : function() {
        var self = this;

        // Braintree
        self._submitButton = document.querySelector('#braintree-submit-button');

        var dropin = braintree.dropin.create({
            authorization: 'xxx', // @TODO change
            selector: '#braintree-dropin',
            paypal: {
                flow: 'checkout',
                amount : this.getMoney(),
                currency: 'EUR'
            }
        }, function (err, dropinInstance) {
            if (err) {
                // Handle any errors that might've occurred when creating Drop-in
                Rollbar.error("Braintree drop-in creation failed.", err);
                return;
            }

            self._submitButtonEventCallback = function() {
                dropinInstance.requestPaymentMethod(function (err, payload) {
                    if (err) {
                        Rollbar.info("Braintree error in requesting payment method.", err);

                        self.displayResponse(err, 'bg-danger');
                        return;
                    }

                    // Send payload.nonce to your server
                    self.sendPayload(payload);
                });
            };

            self._submitButton.addEventListener('click', self._submitButtonEventCallback);
        });
    },

    destroyDropin : function() {
        var self = this;
        $dropin = $('#braintree-dropin');

        self._submitButton.removeEventListener('click', self._submitButtonEventCallback);
        self._submitButtonEventCallback = null;

        $dropin.empty();
    },

What I’m doing is I’m using removeEventListener to get rid of the current click handler when destroying it.

Is this a good approach? Pls don’t laugh at my JS, I’m a PHP guy 😄

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
jackellenbergercommented, Jan 10, 2019

Hey @ndabAP, we played around with patching this in the current major version and weren’t really happy with any of the solutions, so we’re planning on fixing this issue the right way™ in the next major version. We don’t have an ETA for that yet, but it is being worked on and should include a solution for this issue. Thanks for your patience!

2reactions
intelliotcommented, Mar 14, 2017

Thanks for asking about this!

We have a teardown() function that you should call to destroy Drop-in. Example:

var dropinInstance;

braintree.dropin.create({
    // ...
}, function (err, dropin) {
    // ...
    dropinInstance = dropin;
});

// Destroy Drop-in
dropinInstance.teardown(function(err) {
    if (err) { console.error('An error occurred during teardown:', err); }
});

We’ll add documentation of this to our README.

Removing the click event listener is fine. Here’s an alternative:

var dropinInstance;

braintree.dropin.create(/* ... */, function(err, dropin) {
    // ...
    dropinInstance = dropin;
});

_submitButton.addEventListener('click', function () {
    dropinInstance.requestPaymentMethod(/* ... */);
});

The click event listener here uses dropinInstance, which you can overwrite at any time.

Does this help?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recreating 'ghost neighborhoods' destroyed by highways
"These highways just ripped through the middle of neighborhoods, disconnecting communities from each other and destroying homes and businesses ...
Read more >
Delete Data vs recreate Pool | TrueNAS Community
Delete Data vs recreate Pool ... I plan on just plain destroying the pool and recreating it fresh before I use ... Massive...
Read more >
Sealing and Destroying Court Records, Vacating Convictions ...
This brochure provides information about sealing and destroying court records, vacating convictions, and deleting criminal history records.
Read more >
MariaDB MaxScale Administration Tutorial
For more information about systemd drop-in files, refer to the systemctl man page and ... the object can be destroyed and recreated in...
Read more >
How to Ensure React is Not Destroying Components
By adding this to each component I suspected, I was able to confirm which components were being destroyed and recreated. I could now...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found