I have a simple vue instance that I'm using to generate a Stripe token.
It looks like this:
var vm = new Vue({
el: '#checkout-form',
data: {
stripe_publishable_key: '{{ config('services.stripe.publishable') }}',
card: {
number: null,
cvc: null,
exp: null,
},
},
ready: function() {
Stripe.setPublishableKey(this.stripe_publishable_key);
},
methods: {
getStripeToken: function() {
Stripe.card.createToken(this.card, this.stripeResponseHandler);
},
stripeResponseHandler: function(status, response) {
// Process response
},
},
});
Basically I pass the card
object in my Vue instance (which is bound to some form inputs) to Stripe's card.createToken
method. This should take the input and call the handler, passing in the response as it does so.
This all works fine except that card.createToken
is modifying the card object and causing the card object on my Vue instance to change - it inserts its own properties and causes strange behaviour on the bound <inputs>
.
Is there a way to pass the card
vue property to Stripe as a copy which is unbound to the vue instance and thus won't change as a result?
Update
In essence this is happening because JavaScript passes objects as a copy-of-a-reference which means the original object can be modified by any function that receives it. The obvious solution is to clone the object by looping or some other means but I was wondering if there's a better way in Vue.