Destroying and recreating the dropin
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:1
- Comments:16 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
Thanks for asking about this!
We have a
teardown()
function that you should call to destroy Drop-in. Example:We’ll add documentation of this to our README.
Removing the click event listener is fine. Here’s an alternative:
The click event listener here uses
dropinInstance
, which you can overwrite at any time.Does this help?