From 754cbdec87a9cf5e1b4a449b439ad7e8ff3bc4fd Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Fri, 13 Jul 2018 22:10:40 +0200 Subject: [PATCH] [mod] send jobs list during websocket initialisation --- run.py | 5 +++++ templates/index.html | 8 +++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/run.py b/run.py index e03c3b6..4a1d3d4 100644 --- a/run.py +++ b/run.py @@ -172,6 +172,11 @@ def subscribe(ws, channel): async def index_ws(request, websocket): subscribe(websocket, "jobs") + await websocket.send(ujson.dumps({ + "target": "init_jobs", + "data": map(model_to_dict, Job.select()), + })) + while True: data = await websocket.recv() print(f"websocket: {data}") diff --git a/templates/index.html b/templates/index.html index 5198df9..6e043f9 100644 --- a/templates/index.html +++ b/templates/index.html @@ -39,10 +39,6 @@ }, }) - $.getJSON("/api/jobs").done(function(data) { - app.jobs = data; - }) - ws = new WebSocket('ws://' + document.domain + ':' + location.port + '/index-ws'); ws.onmessage = function (event) { @@ -50,7 +46,9 @@ var data = message.data; var target = message.target; - if (target == "update_job") { + if (target == "init_jobs") { + app.jobs = data; + } else if (target == "update_job") { for (var i = 0; i < app.jobs.length; ++i) { if (app.jobs[i].id == data.id) { Vue.set(app.jobs, i, data);