Let’s continue our journey of learnning Svelte and JavaScript 😊
This time, we will build a small Web application for movie seat booking. After finishing, we will have a cute demo looks like this:
Svelte onMount
Because we need to keep the selected seat status after page refreshing, we need to use localStorage
to store selected seat indexes , then reloading them when page in reloaded. In Svelte
, we need to use onMount
to hook up a function to reload data:
|
Restore state and event hookup
The function populateUI
will do 3 things:
- reload selected seats and updated seats count
- update selected movie price
- hook up function for each movie seat click event
|
If we changed the selected movie, we need to update the price for a single movie , so another event hookup in HTML:
|
The logic for total price
The function updateSelected
will update new selected movies count. Here, a tricky part is use css selector
to get elements:
|
How do we calculate the total price of movies? A nice feature comes from Svelte may help us reduce code complexity :
|
With this snippet, it means whenever count
and moviePrice
changed, total
will be updated accordingly , nice and simple!
That’s all, the whole code for this demo is at: sveltejs-apps (github.com)
Join my Email List for more insights, It's Free!😋