Week 7 - Still More Vue
Week 7 - Still More Vue
● State Management
● Routes
● SPAs etc.
State Management
UI State
UI = f(State)
State management pattern
● State
○ “Source of truth” about internals of the app
● View
○ function of State - declarative mapping
● Actions
○ view provides input: action
○ state changes in response to action
Problem:
store.commit({
type: 'increment',
amount: 10
})
Actions
● Mutations must be synchronous - no Example:
async calls permitted
○ Some data updates may not be possible to actions: {
sync increment ({ commit }) {
● Actions can contain async functionality commit('increment')
}
○ do not change state directly: commit
}
mutations
store.dispatch('increment')
Why double work?
● Actions can contain actions: {
checkout ({ commit, state }, products) {
async calls
// save the items currently in the cart
const savedCartItems = [...state.cart.added]
// send out checkout request, and optimistically
// clear the cart
commit(types.CHECKOUT_REQUEST)
// the shop API accepts callbacks
shop.buyProducts(
products,
// handle success callback
() => commit(types.CHECKOUT_SUCCESS),
// handle failure callback
() => commit(types.CHECKOUT_FAILURE, savedCartItems)
)
}
}
Composing actions
// assuming `getData()` and `getOtherData()` return Promises
actions: {
async actionA ({ commit }) {
commit('gotData', await getData())
},
async actionB ({ dispatch, commit }) {
await dispatch('actionA') // wait for `actionA` to finish
commit('gotOtherData', await getOtherData())
}
}
Summary
● State management is complex when dealing with multiple components
● Some kind of globally accessible state required
● Controlled mutation important to allow maintainability
Routing
Page composition
● Original:
○ all pages are HTML from server
● Vue-like frameworks:
○ components
○ parts of app can correspond to components instead of HTML pages
○ application - not just sequence of pages?
Example
<div id="app">
<h1>Hello App!</h1>
<p>
<!-- use router-link component for navigation. -->
<!-- specify the link by passing the `to` prop. -->
<!-- `<router-link>` will be rendered as an `<a>` tag by default -->
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>
</p>
Src: https://router.vuejs.org/guide/#javascript
Advantages
● Clickable links to transition between components
○ No need of actual HTML page
● Clicks handled by client JS, no need to hit server
● Can replace parts of existing page - limit refreshes
Dynamic routes
// User component definition
const User = {
template: '<div>User {{ $route.params.id }}</div>'
}
// Dynamic route
const router = new VueRouter({
routes: [
// dynamic segments start with a colon
{ path: '/user/:id', component: User }
]
})
Impact on reactivity
● Navigate from /user/one to /user/two const User = {
template: '...',
reuses same component
watch: {
○ May not trigger reactive updates $route(to, from) {
● Install a watcher on $route object // react to route changes...
}
}
}
More features
● Nested routes
○ router-view inside a component
● Named routes
○ readability and maintainability
● Named views
○ associate multiple components with different router-view by name
● HTML5 history mode
○ push URLs into the history of the browser
○ allow more natural navigation
○ Better user experience - not fundamentally different concept
Why router?
● Routes handled in JS
● Routes associated with components
● Navigation inside a single app - updates from server only on demand
Page
Offline -
Online Web Worker from cache
Server
Characteristics
● Installability
● Web Manifest: metadata to identify to operating system
● WebAssembly:
○ faster operation possible - compiled
● Storage
○ Web storage APIs
● Service workers
Example: https://app.diagrams.net/
Web apps vs Native
● Native:
○ Compiled with SDKs like Flutter, Swift SDK
○ Best access to underlying OS
○ Restrictions minimized with OS support
○ Look and Feel of native, but not uniform across devices
● Web apps:
○ write once, run anywhere (original Java slogan)
○ Simple technologies, low barrier to entry
○ Evolving standards