Babel + JSX
const CartModel = {
items: [],
addItem: item => CartModel.items.push(item),
getSubTotal: () => CartModel.items.reduce(
(total, item) => total + item.price,
0
)
}
const Cart = {
view () {
return [
m('h1', 'Cart'),
CartModel.items.map(item =>
m('p', { style: { display: 'flex', justifyContent: 'space-between' } },
m('code', `#${item.id}`),
m('span', item.name),
m('span', `${item.price} gil`)
)
),
m('strong', `Total: ${CartModel.getSubTotal()} gil`)
]
}
}
const List = {
items: [
{ id: 1, name: 'Anak Meat', price: 80 },
{ id: 2, name: 'Birdbeast Egg', price: 20 },
{ id: 3, name: 'Bulette Shank', price: 220 },
{ id: 4, name: 'Daggerquill Breast', price: 60 },
{ id: 5, name: 'Dualhorn Steak', price: 160 },
{ id: 6, name: 'Garula Sirloin', price: 120 },
{ id: 7, name: 'Gighee Ham', price: 30 },
{ id: 8, name: 'Lucian Tomato', price: 200 },
{ id: 9, name: 'Luncheon Meat', price: 100 },
{ id: 10, name: 'Sheep Milk', price: 10 },
],
view () {
return [
m('h1', 'List'),
m('table[border=1]', { style: { width: '100%', borderCollapse: 'collapse' } },
m('thead',
m('tr',
m('th', 'ID'),
m('th', 'Name'),
m('th', 'Price'),
m('th')
)
),
m('tbody', this.items.map(item =>
m('tr',
m('td', item.id),
m('td', item.name),
m('td', `${item.price} gil`),
m('td', m('button[type=button]', { onclick: event => CartModel.addItem(item) }, 'Buy'))
)
)
)
)
]
}
}
const Layout = {
view (vnode) {
return m('section',
m('nav',
m('a[href=/list]', { oncreate: m.route.link }, ' List '),
m('a[href=/cart]', { oncreate: m.route.link }, ' Cart ')
),
vnode.children
)
}
}
m.route(document.body, '/list', {
...