Read the statement by Michael Teeuw here.
Module shows no text.
-
@k-0 request is an async call, it will return immediately, but the callback function will not happen until a LONG time after the getdom() function returns…
you need to do this someplace else (on a timer maybe)…
and when the request returns, then save the data and signal mm with this.updateDom() to indicate new data is ready for viewing… MM will then call your getDom() routine…
-
Can you (or someone else) give me an example? I have no experience with async programming.
I tried to solve this problem with setTimeout(function() { mycode }, 6000) , but it doesn’t work. -
@k-0
Try this;Module.register("MMM-Test", { defaults: {}, start: function () { this.count = 0 this.timer = null }, getContent: function() { var heute = new Date().toISOString().substr(0,10); var cnt = 0; var url = 'https://openmensa.org/api/v2/canteens/838/days/' + heute + '/meals' fetch(url, {mode:"cors"}) .then((response) => { console.log("Response fetched.") return response.text(); }) .then((text) => { this.parseContent(text) }) .catch((error) => { console.log('Error:', error) }) this.timer = setTimeout(()=>{ this.getContent() }, 30000) }, parseContent: function(text) { var dom = document.getElementById("TEST") dom.innerHTML = "" var data = JSON.parse(text) for (let i = 0; i < data.length; i++) { var item = data[i] var elem = document.createElement("div") elem.innerHTML = `${item.category} - ${item.name}` dom.appendChild(elem) } }, notificationReceived: function(noti, payload, sender) { if (noti == "DOM_OBJECTS_CREATED") { this.getContent() } }, getDom: function() { var dom = document.createElement("div") dom.id = "TEST" return dom } })
-
@Sean doable, but sloppy. Supposed to call updatedDom(), and let mm handle the actual update.
-
@sdetweil
Yes, you’re right. it was just an example. Anyway, it can be done like this;parseContent: function(text) { this.lastPatched = JSON.parse(text) this.updateDom() }, getDom: function() { var dom = document.createElement("div") dom.id = "TEST" if (this.lastPatched) { ... // draw patched data... } return dom }