Read the statement by Michael Teeuw here.
How to pass a value between functions in a Module?
-
Hi,
I’m trying to pass the value in the variable
ajaxResultto thegetDomfunction so that I can render it into a table.I’ve tried to
returnthe value from thestartfunction but that comes up asundefinedHow are values passed between these functions?
Thanks for the help in advance
`/* global Module */
Module.register(“trainScheduler”,{
// Default module config. defaults: { text: "| --- | --- | --- | --- |" }, currentWeatherType: "", // Define required scripts. getScripts: function() { return ["moment.js"]; }, // Define start sequence. start: function() { Log.info("Starting module: " + this.name); var ajaxResult; // Log.info('RESULT' + ajaxResult); if (this.config.remoteFile != null) { this.trainSchedule( (response) => { this.config = JSON.parse(response); //function call to send the result to out! responseRunner(response); } ); function responseRunner (response) { ajaxResult = response; }; } // Schedule update timer. // LEMME COME BACK TO THIS AUTO REFERESHER THING // var self = this; // setInterval(function() { // self.updateDom(self.config.fadeSpeed); // }, this.config.updateInterval); }, /* complimentFile(callback) * Retrieve a file from the local filesystem */ trainSchedule: function(callback) { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/json"); xobj.open("GET", this.file(this.config.remoteFile), true); xobj.onreadystatechange = function() { if (xobj.readyState == 4 && xobj.status == "200") { callback(xobj.responseText); } }; xobj.send(null); }, // Override dom generator. getDom: function() { var complimentText = 1; var compliment = document.createTextNode(complimentText); var wrapper = document.createElement("div"); wrapper.className = this.config.classes ? this.config.classes : "thin xlarge bright"; wrapper.appendChild(compliment); return wrapper; },});
` -
@ameenaziz Have a look at how the default modules do it, like
newsfeed. It creates athis.newsItemsvariable, but also athis.loadedboolean. It usessocketNotificationRecieved()to triggergenerateFeed()which updatesthis.newsItems, then changesthis.loadedto true. -
@ninjabreadman thanks for the guidance, the new modules is pretty complex, is there a simpler module to take an example from?
All I’m really after is the data produced from the
trainSchedulefunction to be accessible by thegetDomfunction. -
@ameenaziz I think if you change
var ajaxResult;tothis.ajaxResult = "";and change all other references (fromajaxResulttothis.ajaxResult), you should be able to access it from anywhere in your module, includinggetDom(). Just remember whengetDom()is first called,this.ajaxResultmay still be empty until the response is received. -
getDom: function()is called initially to set up the content. Usually you have something likediv.innerHTML = variable;but at the beginning, varibable might be empty. To make the variable usable globally (in the scope of your module), you set it up and call it as this.variable.When your function that fills this variable updates that variable content, you call
updateDom();
That makes the dom reload and this time, this.variable has content to show.There’s other ways to fill the element with the content of a variable without totally rebuilding the DOM:
In the function that handles the variable, you could saydocument.getElementbyId(elementId).innerHTML = variable;Edit: To complete this. If you want to pass a value from one function to another (but this is not working for the Dom, because that has to be set up initially):
firstFunction: function () { var greeting: "Hello"; secondFunction(greeting); } secondFunction: function (word) { var greeting: word; Log.info(greeting); // "Hello" }
