Read the statement by Michael Teeuw here.
Canteen-module // Mensa-Modul
-
Where do you put this code? You have to put require in node_helper.js, not the module js file.
Also check the log files in the developer tools in e.g. Chrome. -
@k-0
I like the module idea very much. Could help some people around here.So your problem is handling the asynchrous call? Is that right?
I think, the node_helper function is ideal to help with this (at least for a beginner).
What I would do, as someone who has his own problems with asynchronous calls:start function:
start: function() { Log.info("Starting module: " + this.name); // Set locale. this.sendSocketNotification("CONFIG", this.config); var self = this; setInterval(() => { self.updateDom(); }, self.config.updateInterval); },
in node_helper.js:
socketNotificationReceived: function(notification, payload) { if (notification === 'CONFIG') { this.config = payload; this.collectData(); self = this; setInterval(function () { self.collectData(); }, 10 * 60 * 1000); } }, collectData: function () { request(url, function (error, response, body) { ..... self.sendSocketNotification("DATA", response); }); } });
You basically have two separate intervals: The “async call” interval in node_helper and the dom update interval in your main js.
After the first asynchronous call you have always some data to show in the dom update interval. This data gets overwritten whenever node_helper sends a socketNotification.Have a look at my rather short module using an asynchronous call:
https://github.com/lavolp3/MMM-CelebrationsIf you upload your module on github we can have a better look at the code and make suggestions.
Hope it helps.
-
I couldn’t find my old code (node_helper.js and module.js files) from my first attempts, but i created new files and merged the code from @lavolp3.
Here is the github url: https://github.com/k-0/MMM-CanteenYes, the main problem was handling the asynchrous call. But i’m pretty sure, that there are more problems. :P
-
@k-0 said in Canteen-module // Mensa-Modul:
the main problem was handling the asynchrous call.
right… you should NOT do this IN the getDom() function…
the timer should fire off this routine… and once the request() has returned, save the data in some variable,
and THEN call updateDom(), which will cause getDom() to be called to format the data -
@k-0 Hi k-0.
So, I liked the idea.
So sat down and, well, basically wrote a new module :-)
I’ve sent a PR, let me know your comments.There’s still lots to do especially the readme :-)
And lots of things to configure.
I can’t use the module, my studies are 10 years back, but I think there may be some people who can benefit from it so I’m interested in bringing this forward.Ideas:
- option to show the whole week
- option to filter meals (leave out vegetarian food, LOL)
Cheers,
Dirk -
Doesn’t look too shabby but lots of styling to be done I guess
-
Hi Dirk,
I could test it today and I really love it! Thank you so much!
I already updated the ReadMe and added the “€” to the price in the .njk-file.
It would be nice to have the date in the header on which the meal refers. :P
Cheers,
KevinEDIT: I think that a option to show the whole week is nice, especially for ppl who have only one or two meals per day. Otherwise it will be to much informations to show.
-
@k-0
The “header” from the config is nothing but a html element.
You could leave the header column out of the module definition and include it separately in your .njk.
Then you would need it to be given a name by the user (via the config) since it is not thrown out by the API.Then just put a
tag above the rest and include the canteen name and a date. < header >{{ config.canteenName }}, {{ moment().format("dd, DD.MM.) }} < /header >
-
Hi @lavolp3 ,
i added
<header>{{ config.canteenName }}, {{ moment().format("dddd") }}</header>
to my MMM-Canteen.njk. But I always get “undefined”.
It works with
{{ config.canteenName }}
It seems there is a problem with moment() in the header. Do you have an idea?
-
@k-0 I am not 100% sure but I think moment() does not work in a nunjucks environment. So what I said above may have been very wrong, sorry!
I can’t test it now, but you can confirm the error with the moment() function by opening the mirror in your browser, then opening the developers console (F12) and looking in the console for an error thrown out by the template.
The nunjucks template works with
- usual html code
- variables you provide it with in the getTemplateData function
- filters (which are functions that need to be bound to functions in the main js file)
So what you can do is to include the variable in the getTemplatedata function
today: moment().format("dddd")
then call it in the template using
< header >{{ config.canteenName }}, {{ today }} < /header >