Using getters and setters Part 2

This is second part in the series to make use of getters and setters in an AlpineJS object, that converts INR to USD.

Well, only a setter has been used here where a function call is made to the setter priceInINR when the textbox of the USD value changes. The setter changes the value of the product's (which is an argument to priceInINR) price_INR value.

x-on:input="$store.global_data.priceInINR = product"

And the setter :

set priceInINR(p)
{            
    const product = this.products.find(product => product.name === p.name);
    product.price_INR = p.price_USD * 83;
},

Here's the full source code.

PS: 1 USD = 83 INR as of 17th February 2024.

Here's a more interactive example / demo : https://anjanesh.s3.amazonaws.com/demo/alpine/alpine-auto-update-variable-method-1.html

0
Subscribe to my newsletter

Read articles from Anjanesh Lekshminarayanan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anjanesh Lekshminarayanan
Anjanesh Lekshminarayanan

I am a web developer from Navi Mumbai working as a consultant for cloudxchange.io. Mainly dealt with LAMP stack, now into Django and trying to learn Laravel and Google Cloud. TensorFlow in the near future. Founder of nerul.in