diff --git a/assets/js/common.ts b/assets/js/common.ts index effcd65..4608fc0 100644 --- a/assets/js/common.ts +++ b/assets/js/common.ts @@ -29,10 +29,10 @@ export function config (api, polling): Config { export const pvapp = { run(conf: Config) { appdata.conf = conf - createApp(appdata).mount() if (appdata.conf.pollurl && !appdata.conf.nopoll) { appdata.poll() } + createApp(appdata).mount() } } @@ -49,12 +49,13 @@ const appdata = { Data } -// components -// (move to separate module(s)) +// Data component +// (move to comp_data.ts) function Data(name, action, domain: string): object { domain = domain || this.conf.domain const comp = { + id: this.conf.itemid, name: name, // -> class of incoming/outgoing message action: action, // (default) action of outgoing message domain: domain, // domain of incoming/outgoing message @@ -68,10 +69,14 @@ function Data(name, action, domain: string): object { exec // default function to execute upon button click } this.components[name] = comp + if (comp.id) { + sendMsg(this.conf, [domain, 'query', name, comp.id], {}) + } return comp } -// appdata and component method definitions +// Data methods +// (move to comp_data.ts) function mounted(name: string, meta: any) { if (meta.defexpr) { @@ -120,6 +125,8 @@ function exec(action: string) { sendMsg(conf, [this.domain, action, this.name, data.id], data) } +// App (appdata) methods + function handle(msg) { const data = JSON.parse(msg.payload) const [ domain, action, class_, item ] = msg.path.split('/') @@ -129,6 +136,8 @@ function handle(msg) { return } if (comp) { + // TODO: use more generic comp.update(data) + // ... or even more generic comp.handle(msg) Object.assign(comp.data, data) } } @@ -137,7 +146,8 @@ function poll() { dopoll(this) } -// helper functions for data manipulation +// helper functions for data manipulation (used by Data component) +// (move to comp_data.ts) function setQuery(obj) { for (const key in obj.data) {