hugo-theme-cyberscopes/assets/js/comp-list.ts

44 lines
1.1 KiB
TypeScript

// list-data.ts - definition and methods of the List component
import { sendMsg } from './common'
// List component
export function List(name, action, domain: string): object {
domain = domain || this.conf.domain
const comp = {
id: this.conf.itemid,
name: name, // also used as class of incoming/outgoing message
action: action, // (default) action of outgoing message
domain: domain, // domain of incoming/outgoing message
data: [], // model = list of rows
meta: {}, // metadata (params) for each field (table column)
handle, // handle incoming messages
initField // initialize field (= @mounted)
}
this.components[name] = comp
sendMsg(this.conf, [domain, 'query', name], {})
return comp
}
// Data methods
function handle(domain, action, class_, item: string, payload) {
if (action == 'list') {
const rows = payload.split('\n')
//this.data = []
for (const row of rows) {
this.data.push(JSON.parse(row))
}
console.log('data: ', this.data)
}
}
function initField(name: string, meta: any) {
if (meta.defexpr) {
meta.default = eval(meta.defexpr)
}
this.data[name] = meta.default || ""
this.meta[name] = meta
}